≡ Menu

Simple way to run a command/cmdlet parallely on multiple remote computers using Powershell

Powershell provides some sort of parallelism with RunSpaceFactory. It give good control over how many PS instances you want to create at any point of time, etc. It is very good(at the same time complex) and useful. Recently I came across another method in powershell which provides parallelism in a simple way. This is available only in Powershell V3. It has got a very good and large set of new features and one of them is WorkFlows(oh! I love it). When a Foreach is used inside workflow, it is allowing to given -Parallel option and it is doing the trick. That means, to utilize this parallelism you need to write a work flow. Don’t worry that is not too hard and we can write very simple and basic work flow to utilize this new feature.

Here I am giving a simple example on how to use this parallelism in Powershell V3. Here I am querying spooler service status on a list of remote computers using Workflow and I am get them the status in just a single shot.

Try the blow code in Powershell v3 and you will realize what I am saying. But the caveat here is, we can not control how many things it should execute in parallel. If you provide a array of 100 computers, then it will execute on all of them at time t — not sure how much time it will take to complete(I am still researching that).

WorkFlow Run-InParallel {            
param(            
[string[]]$ComputerName            
)            
    foreach -Parallel ($Computer in $ComputerName) {            
        $ser = Get-Service -PSComputerName $Computer -Name Spooler            
        "Status of {0} service on Computer {1} is {2}" -f $ser.Name, $Computer, $ser.Status            
    }            

}            

Run-InParallel -ComputerName PC1, PC2, PC3

Hope this tip Helps you. Feel free to comment if you have any suggestions or questions.

Comments on this entry are closed.

  • George February 8, 2013, 10:06 pm

    You could potentially take down your LAN/WAN/Workstations by pushing out a request which either overburdens the workstations and/or the bandwidth. The potential is awesome though. Have you found any way of limiting the amount to be processed yet?

    • Sitaram Pamarthi February 15, 2013, 11:21 am

      Yes, I agree. I haven’t investigated anything solid yet. I will let you know if I come across some solution for this. Alternative is to use RunSpaceFactory which provides thresholds.

  • Mark January 27, 2015, 6:40 pm

    Hi. I have a question on how to run foreach -Parallel command on multiple servers from a file .
    Thanks in advance

    • TechiBee February 2, 2015, 7:00 pm

      You can use like this.

      Run-InParallel -ComputerName (get-content c:\temp\servers.txt)