等待工作不等待
问题描述:
这里是代码等待工作不等待
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.
如何解决?
答
请勿将Invoke-Command的输出传递给Out-Null。如果你这样做,那么$SetupJob
将变为空,Wait-Job将立即返回。
这有效,但它输出到Powershell ISE控制台。如何抑制这一点? – Glowie 2014-09-29 19:20:21
@Glowie我认为你想将Wait-Job的输出传递给Out-Null,而不是Invoke-Command的输出。 – 2014-09-29 19:26:43