2016-10-27 84 views
0

我運行此PowerShell腳本,並且它在PowerShell 4.0上正常工作。但是我現在有PowerShell的5.0和腳本的工作,但它拋出一個錯誤:測試路徑移動 - 項目問題

腳本:

$path = "X" 
$destination = "Y" 

while (Test-Path -Path $path) { 
    Move-Item -Path "$path\*zip" -Destination "$destination" 
    } 

我得到的錯誤是:

Move-Item : The process cannot access the file because it is being used by another process.

回答

0

就去做

Move-Item -Path "$path\*zip" -Destination "$destination" -ErrorAction Ignore 
0

問題的標題: 「測試路徑移動項目問題」意味着一個cmdlet可能會影響另一個cmdlet。這對我來說沒有意義,因爲Test-Path正在檢查文件夾的存在,並且Move-Item正在處理該文件夾中的子項目。

我個人使用while循環用於該用途的情況下,一旦你已經確定路徑存在,你並不需要保存測試它:

if(Test-Path -Path $path){ 
    Move-Item -Path $path\*zip -Destination $destination 
}