mitomed
mitomed

Reputation: 2066

How to check if a background job has timed out in powershell

I have this as part of a script

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

And I would like to raise an error (is part of an octopus deploy step) if it's timed out

Thanks

Upvotes: 4

Views: 748

Answers (1)

Brian Reynolds
Brian Reynolds

Reputation: 411

How about something like this:

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

Upvotes: 5

Related Questions