2013-05-10 16 views
0

我在調用SSIS包的字符串數組中有多個命令。 爲了利用硬件,我希望一次運行多個軟件包,等待一個完成後再添加另一個軟件包。通過觀察上的其他類似的問題,我覺得像這樣的工作:Powershell中多處理exec的高達閾值

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 
} 

這不管理,打開記事本6分的,但它不會在某個閾值等待 - 在本例中2。理想情況下,它應該等到我關閉其中一個記事本(即過程結束)。

以前有人會這樣做嗎?

回答

0

你已經很近了,但是你並沒有用Start-Job來啓動這個過程,所以Get-Job和Wait-Job沒有任何好處。這會工作嗎?

$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 
}