質問

I am having trouble with throttling of jobs and "hung" or "failed" jobs. Here is basically what I am trying to do.

$allServers = Import-Csv "C:\temp\input.csv"
$job = $allServers | % {
    while (@(Get-Job -State Running).Count -ge 6) {
        Start-Sleep -Seconds 2
    }
    Start-Job -Name $_.computerName -ScriptBlock {
        param ($cpn,$dom)
        (DO QUERIES HERE)
        (OUTPUT TO OBJECT HERE)
    } -ArgumentList $_.computerName,$_.Domain 
}
$jobsdone = $job | Wait-Job | Receive-Job

I would like to run 5 concurrent jobs, simple enough.

The issue is when I query a server that does not respond, the job hangs and the script never ends. I have tried adding...

Wait-Job -Name $_.computerName -Timeout 20

...above the last curly brace, but all that does is effectively limit the concurrence to one thread, until 20 seconds goes by, then abandoning the hung job to do other jobs. The whole script still doesn't finish in that instance.

This code works fine without the throttling and job waiting, so long as I don't get a non-responsive server.

役に立ちましたか?

解決

Inside your while loop check the length of time the job has been running. If that is greater than some timeout you determine, then stop that job e.g.:

while (@(Get-Job -State Running).Count -ge 6) {
    $now = Get-Date
    foreach ($job in @(Get-Job -State Running)) {
        if ($now - (Get-Job -Id $job.id).PSBeginTime -gt [TimeSpan]::FromMinutes(2)) {
            Stop-Job $job
        }
    }
    Start-Sleep -sec 2
}

You might want to check out this PowerShell team blog post on how to throttle jobs using a queue.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top