[英]Powershell New-Item Command
我添加了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文件:
Col A Col B
Test C:\Users\US6205\Desktop\Test\Unprocessed
Test1 C:\Users\US6205\Desktop\Test1\Unprocessed
以下行中管道的使用不正确:
$Dest = $Path | New-Item -path $Dest -itemtype directory | Move-Item -Path $Source -Destination $Dest -Force
这不会将$ Path分配给$ Dest。 它将$ Path变量传递到管道右侧的下一个命令。 尝试在New-Item命令中使用时,$ Dest为空。 相反,请在以下各行中使用:
$Dest = $Path
New-Item -path $Dest -itemtype directory
Move-Item -Path $Source -Destination $Dest -Force
我还建议使用Test-Path
cmdlet来检查脚本中是否存在路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.