等待工作不等待

时间:2014-09-29 18:47:04

标签: powershell wait

这是代码

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在remote_computer上执行setup.exe。执行后,它应该被删除。但是,我得到以下

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.

如何解决?

1 个答案:

答案 0 :(得分:2)

不要将Invoke-Command的输出传递给Out-Null。如果你这样做,那么$SetupJob将为null并且Wait-Job将立即返回。