Sune
Sune

Reputation: 3280

Resubmit hanging job (start-job)

I am starting several jobs (with Start-Job) and at the end of my script i do a check to see if the jobs have been running more than X seconds. I would then like to take the Running and Failed jobs and restart them until they succeed.

The jobs are named after the server that I'd like to run against (with for example Test-Connection). My problem is that I can't figure out how to re-submit the jobs!

get-job | where { $_.state -eq "running" } | remove-job -force | start-job -ScriptBlock { echo $_ }

Kind regards:)

Upvotes: 3

Views: 1625

Answers (1)

Andy Arismendi
Andy Arismendi

Reputation: 52699

One way to restart failed jobs:

Start-Job -Name Foo -ScriptBlock {
    $ErrorActionPreference = 'Stop'
    Get-Item C:\DoesNotExists 
} | Wait-Job > $null

Get-Job | ? { $_.State -eq 'Failed' } | % {
    Start-Job -Name $_.Name -ScriptBlock { iex $args[0] } -ArgumentList $_.Command | Wait-Job | Receive-Job
}

Upvotes: 3

Related Questions