我在New-Item命令中添加了它,但它不喜欢我的语法。请帮忙:
$Dir = get-childitem -path "\\ahs-bind01\ftptest01\CRAR" -recurse
$Source = $Dir | where {$_.extension -eq ".txt"}
#Declare the file path and sheet name
$file = "C:\Users\us6205\Desktop\DatabaseNameConfigs\Test\CareMoveFileParentDestPaths.xlsx"
$sheetName = "sheet1"
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowLOC,$colLOC = 1,1
$rowUNC,$colUNC = 1,2
#loop to get values and store it
for ($i=1; $i -le $rowMax-1; $i++)
{
$LOC = $sheet.Cells.Item($rowLOC+$i,$colLOC).text
$UNC = $sheet.Cells.Item($rowUNC+$i,$colUNC).text
$Path = Get-ChildItem $UNC -recurse |
Where-Object { ($_.PSIsContainer -eq $true) -and ( $_.Name -match "$Source") }
$Dest = $Path | New-Item -path $Dest -itemtype directory | Move-Item -Path $Source -Destination $Dest -Force
}
#close excel file
$objExcel.quit()
Excel Doc:
Col A Col B
Test C:\Users\US6205\Desktop\Test\Unprocessed
Test1 C:\Users\US6205\Desktop\Test1\Unprocessed
答案 0 :(得分:3)
管道的使用在以下行中不正确:
$Dest = $Path | New-Item -path $Dest -itemtype directory | Move-Item -Path $Source -Destination $Dest -Force
这不会将$ Path分配给$ Dest。它将$ Path变量传递给管道右侧的next命令。当您尝试在New-Item命令中使用时,$ Dest为空。相反,请使用如下单独的行:
$Dest = $Path
New-Item -path $Dest -itemtype directory
Move-Item -Path $Source -Destination $Dest -Force
我还建议使用Test-Path
cmdlet检查脚本中是否存在路径。