PowerShell Job Queue PowerShell Job Queue powershell powershell

PowerShell Job Queue


PowerShell will do this for you if you use Invoke-Command e.g.:

Invoke-Command -ComputerName $serverArray -ScriptBlock { .. script here ..} -ThrottleLimit 5 -AsJob

BTW I don't think your use of a .NET Queue is going to work because Start-Job fires up another PowerShell process to execute the job.


You may take a look at the cmdlet Split-Pipeline of the module SplitPipeline.The code will look like:

Import-Module SplitPipeline$MaxJobs = 5$list = Get-Content ".\list.csv"$list | Split-Pipeline -Count $MaxJobs -Load 1,1 {process{    # process an item from $list represented by $_    ...}}

-Count $MaxJobs limits the number of parallel jobs. -Load 1,1 tells to pipeexactly 1 item to each job.

The advantage of this approach is that the code itself is invoked synchronouslyand it outputs results from jobs as if all was invoked sequentially (evenoutput order can be preserved with the switch Order).

But this approach does not use remoting. The code works in the current PowerShell session in several runspaces.