Question

I have multiple commands in a string array that invoke SSIS packages. To take advantage of the hardware I want to run many packages at once, waiting until one completes before adding another to the fray. From looking at other similar questions on SO I think something like this would work:

cls
$commands = @()
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"

foreach ($instance in $commands)
{
    $running = @(Get-Job | Where-Object { $_.JobStateInfo.State -eq 'Running' })
    if ($running.Count -le 2) 
    {
        Start-Process $instance    
    } 
    else 
    {
         $running | Wait-Job
    }

    Get-Job | Receive-Job
}

This does manage to open 6 notepad's, but it doesn't wait at some threshold - 2 in this example. Ideally it should wait until I close one of the notepad's (i.e. the process finishes).

Would anyone have done this before?

Was it helpful?

Solution

You're close, but you're not starting the process with Start-Job, so Get-Job and Wait-Job don't do any good. Would this work?

$commands = @()
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"
$commands += "notepad.exe"

foreach ($instance in $commands)
{
    While ( (Get-Job -State Running).Count -ge 2 )
    { Start-Sleep -s 5 } 

    Start-Job -ScriptBlock {Start-Process -FilePath $args[0]} -ArgumentList $instance
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top