Techibee.com

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.

Exit mobile version