Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Monday, November 26, 2012

Executing a lot of code on multiple clients

I took a question today about how to execute a bunch of code on remote clients.  The users code worked for them on their local client, but they needed it to execute remotely.  This is a job for Invoke-Command!!!

Invoke command will let you take your existing code and run it on multiple clients.  Here is a basic example.

invoke-command -ScriptBlock {

   #<< Your code here >>

   } -ComputerName #<<Name of remote computer>>

 

Now for the part about executing this code against multiple clients.

 

$Servers = “SVR1”, “SVR2”, “SVR3”

ForEach ($Server in $Servers)

{

invoke-command -ScriptBlock {

   #<< Your code here >>

   } -ComputerName $Servers

}

 

In this example, we nest our invoke command  inside of a ForEach statement.  We provide a list of server names to $Servers and use this to cycle through each client.  You may want to add some error handling to this code.

No comments: