Powershell Throttle Multi thread jobs via job completion Powershell Throttle Multi thread jobs via job completion powershell powershell

Powershell Throttle Multi thread jobs via job completion


You can test the following :

$servers = Get-Content "C:\temp\flashfilestore\serverlist.txt"$scriptBlock = { #DO STUFF }invoke-command -computerName $servers -scriptblock $scriptBlock -jobname 'YourJobSpecificName' -throttlelimit 4 -AsJob

This command uses the Invoke-Command cmdlet and its AsJob parameter to start a background job that runs a scriptblock on numerous computers. Because the command must not be run more than 4 times concurrently, the command uses the ThrottleLimit parameter of Invoke-Command to limit the number of concurrent commands to 4.

Be careful that the file contains the computer names in a domain.


In order to avoid inventing a wheel I would recommend to use one of theexisting tools.

One of them is the scriptInvoke-Parallel.ps1.It is written in PowerShell, you can see how it is implemented directly. It iseasy to get and it does not require any installation for using it.

Another one is the module SplitPipeline. It may work faster because it is written in C#. It also covers some more usecases, for example slow or infinite input, use of initialization and cleanup scripts.

In the latter case the code with 4 parallel pipelines will be

$servers | Split-Pipeline -Count 4 {process{ <# DO STUFF on $_ #> }}


I wrote a blog article which covers multithreading any given script via actual threads. You can find the full post here:

http://www.get-blog.com/?p=189

The basic setup is:

$ISS = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()$RunspacePool = [runspacefactory]::CreateRunspacePool(1, $MaxThreads, $ISS, $Host)$RunspacePool.Open()$Code = [ScriptBlock]::Create($(Get-Content $FileName))$PowershellThread = [powershell]::Create().AddScript($Code)$PowershellThread.RunspacePool = $RunspacePool$Handle = $PowershellThread.BeginInvoke()$Job = "" | Select-Object Handle, Thread, object$Job.Handle = $Handle$Job.Thread = $PowershellThread$Job.Object = $Object.ToString()$Job.Thread.EndInvoke($Job.Handle)$Job.Thread.Dispose()