如何检查后台作业是否在powershell中超时

时间:2015-06-03 13:41:29

标签: powershell

我将此作为脚本的一部分

$timeoutSeconds = $timeoutMinutes * 60
$job = Start-Job -ScriptBlock $block -ArgumentList @($environment, $filter)
Wait-Job $job -Timeout $timeoutSeconds 
Stop-Job $job 
Remove-Job $job

如果超时,我想提出错误(是章鱼部署步骤的一部分)

由于

1 个答案:

答案 0 :(得分:4)

这样的事情怎么样:

Wait-Job $job -Timeout $timeoutSeconds
if ($job.state -eq 'Running') {
    Write-Error "Job timed out but did not complete."
}
$jobResults = Receive-Job $job
$jobResults

Stop-Job $job 
Remove-Job $job