2014-09-29 205 views
0

這裏是代碼等待工作不等待

try{ 
    $SetupJob = invoke-command -ComputerName $j -Credential $credentials -ScriptBlock $sb -AsJob | Out-Null 

} 
catch [System.Exception]{ 
    continue 
} 

$SetupJob|Wait-Job 


$disable_command = "D:\PSTools\PsExec.exe $comp -u Administrator -p $str -accepteula powershell.exe c:\share\ps_disable.ps1" 
$CmdOutput = Invoke-Expression $disable_command 2>&1 



Remove-Item Z:\ps_*able.ps1 
Remove-Item Z:\setup.exe 

$SetupJob執行setup.exe上REMOTE_COMPUTER。執行後,它應該被刪除。但是,我得到以下

Remove-Item : Cannot remove item \\remote_computer\share\setup.exe: The process cannot access the file '\\remote_computer\share\setup.exe' because it is being used by another process. 

如何解決?

回答

2

請勿將Invoke-Command的輸出傳遞給Out-Null。如果你這樣做,那麼$SetupJob將變爲空,Wait-Job將立即返回。

+0

這有效,但它輸出到Powershell ISE控制檯。如何抑制這一點? – Glowie 2014-09-29 19:20:21

+1

@Glowie我認爲你想將Wait-Job的輸出傳遞給Out-Null,而不是Invoke-Command的輸出。 – 2014-09-29 19:26:43