1

$vcconnect has 50 machines and I need to run this job on all 50 machines, but when I just run it, it crashes the shell.

I would like to limit the parallel execution to 10 at a point of time.

I tried a do-while but I was missing something as it executed on all 50 at same time and crashed my shell.

foreach($vci in $vcconnect){ 
    [array]$jobstart += Start-Job -Name top2 -ArgumentList @($vci, $cred, $from,  $to) -ScriptBlock $importcode
}
Draken
  • 3,134
  • 13
  • 34
  • 54
SUN
  • 87
  • 1
  • 12
  • Thanks to Tessellating Heckler for pointing to right one. – SUN Feb 28 '17 at 23:14
  • Re-opened this question. Workflows are not an optimal solution for parallel jobs, and provides no facility for explicitly limit the number of concurrently running jobs – Mathias R. Jessen Feb 28 '17 at 23:19

1 Answers1

1

If you want to run scripts in parallel, and control the maximum number of concurrently running instances, use a RunspacePool:

# Create a RunspacePool, of maximum 10 concurrently running runspaces
$RSPool = [runspacefactory]::CreateRunspacePool(1,10)
$RSPool.Open()

# Start a new "job" for each server
$Jobs = foreach($vci in $vconnect){
    $PS = [PowerShell]::Create().AddScript($importcode)
    $PS.RunspacePool = $RSPool
    $vci, $cred, $from,  $to |ForEach-Object {
        [void]$PS.AddArgument($_)
    }
    New-Object psobject -Property @{
        Shell = $PS
        ComputerName = $vci
        ResultHandle = $PS.BeginInvoke()
    }
}

# Wait for the "jobs" to finish
do{
    Start-Sleep -Milliseconds 500
} while ($Jobs |Where-Object IsCompleted -eq $false)

# Collect results, suppress (but warn on) errors
$Results = foreach($Job in $Jobs){
    $Job.Shell.EndInvoke($Job.ResultHandle)
    if($Job.Shell.HadErrors){
        Write-Warning "$($Job.ComputerName) had $($Job.Shell.Streams.Error.Count) errors:"
        $Job.Shell.Streams.Error |Write-Warning
    }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • 1
    For the waiting bit, instead of a `Do/While` loop you could just use `[System.Threading.WaitHandle]::WaitAll($Job.ResultHandle.AsyncWaitHandle)` – TheMadTechnician Mar 01 '17 at 00:03