确认子目录的存在

时间:2017-06-29 19:58:59

标签: powershell powershell-v4.0

我正在编写一个脚本来查询SQL服务器以聚合可用于搜索各种目录的数据。如果文件夹不存在,则应将代码末尾的文本文件中不存在的路径写入。 我创建了一个数组来预填充使用SQL数据完成的路径的字符串。我正在讨论输入sting的问题,格式不正确,而且它的问题没有正确填写文本文档。它将使用既包含也不存在的文件夹填充文本文档。 我尝试了各种配置。我相信阵列可能是我的问题但我目前不确定。

错误如下:

Cannot convert value "Walker" to type "System.Int32". Error: "Input string was not in a correct format."
At \\cottonwood\users\C.B\My Documents\Untitled1.ps1:36 char:115
+ ... Address)#> + $($Row.'Last Name') + $array[$i]
+                    ~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastFromStringToInteger

代码如下:

$SQLServer = "REDWOOD" #use Server\Instance for named SQL instances! 
$SQLDBName = "MARS"
$SqlQuery = "select Account, IsActive, [Last Name] FROM vw_loans WHERE ( Account NOT IN ('100040A','100041A','100044A','100044B','100044C','100079A','100040A','100041A','100044A','100044B','100044C','100079A','100153B','100413B')) AND LEFT(Account,1)<>'_' AND (Account NOT like '%B%') AND (LoanStatus != 'PRELIM') ORDER BY Account"

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)


$SqlConnection.Close()

clear

$DataSet.Tables[0]

$array =   "\I. Originations\Final Originations Package","\II. Servicing\A. Communications", "\II. Servicing\B. Foreclosure Documents","\II. Servicing\C. Bankruptcy Documents", "\II. Servicing\D. Amendments & Extensions", "\II. Servicing\E. Property", "\II. Servicing\F. Previous Servicer Data", "\III. Loan Documents", "\IV. Taxes, Insurance, HOA\HOA", "\IV. Taxes, Insurance, HOA\Insurance","\IV. Taxes, Insurance, HOA\Insurance\PMI","\IV. Taxes, Insurance, HOA\Taxes"

foreach ($Row in $dataset.Tables[0].Rows) 
{ 

   for($i=0;$i -lt $array.Length; $i++)
    { 
     if($Row.IsActive -eq $True)      
        {
              $CorrPath = "U:\Shared\Loan Documents - Active\" + $($Row.Account) + " - " + <#$(Row.Address)#> + $($Row.'Last Name') + $array[$i]
        }    

     if($Row.IsActive -eq $False)
         {
           $CorrPath = "U:\Shared\Loan Documents - Inactive\" + $($Row.Account) + " - " + <#$(Row.Address)#> + $($Row.'Last Name') + $array[$i]
         }

    $FileExist = Test-Path $CorrPath

    If($FileExist -eq $False) 
    {Add-Content $Corrpath -Path "\\cottonwood\users\IT\Missing Folder Location\MissingSubFolders.txt"}

    }



}

1 个答案:

答案 0 :(得分:1)

在我看来,解析器对+运算符应该做的事情感到困惑。它应该在连接时尝试添加。尝试将所有内容强制转换为字符串:

if($Row.IsActive) {
    $CorrPath = "U:\Shared\Loan Documents - Active\" + $($Row.Account.ToString()) + " - " + <#$(Row.Address)#> + $($Row.'Last Name'.ToString()) + $array[$i].ToString()
} else {
    $CorrPath = "U:\Shared\Loan Documents - Inactive\" + $($Row.Account.ToString()) + " - " + <#$(Row.Address)#> + $($Row.'Last Name'.ToString()) + $array[$i].ToString()
}

或者尝试以不同方式格式化文件名:

$CorrPath = "U:\Shared\Loan Documents - Active\{0} - {1}{2}" -f $Row.Account,$Row.'Last Name',$array[$i]