2017-02-10 69 views
0

我在PowerShell中使用robocopy,如果文件已成功複製,robocopy會返回退出代碼1,這會告訴PowerShell失敗。有沒有好的做法可以忽略錯誤,因爲它不是錯誤?獲取PowerShell忽略退出碼?

$source = "$env:buildlocation\Reports\$env:ctrelname\DiskImages\DISK1"; 
$target = "$env:builddestination\$env:ctrelver\$env:ctrelname\Reports"; 
$robocopyOptions = @('/e', '/r:0', '/np'); 
Get-ChildItem -Path $source -Directory -Attributes !Hidden | 
    Sort-Object -Property CreationTime | 
    ForEach-Object { & robocopy.exe $source $target $robocopyOptions }; 
echo $LASTEXITCODE 
+0

請參閱[此問題](http://stackoverflow.com/questions/13883404/custom-robocopy-progress-bar-in-powershell)。 – sodawillow

+1

你爲什麼需要這個?除非您明確處理退出代碼,否則從外部命令退出代碼對PowerShell無關緊要。 –

回答

0

如上所述PowerShell不評估本機命令的錯誤代碼。它只存儲在$LastExitCode。在$LastExitCode

Invoke-Expression 'cmd /c "exit 5"' 

PowerShell中只存儲5:試試這個簡單的例子。例如