Stuart Moore shows an easy way to execute a Powershell script against multiple servers:
We setup new PsSessions using
New-PsSession, I setErrorActiontoSilentlyContinuejust in case a host isn’t available for some reason (if I was being good I’d try/catch here).As we’re just using PS standard functionality here with
Get-Servicethere’s no need to build a a new function, we can just call this directly here. By callingInvoke-Commandagainst a session pointed at numerous hosts we can PowerShell handle all the connection management here and just assume the command will be ran against each host. if we were running against a lot of hosts then we would want to look into using the-ThrottleLimitparameter to limit the number of concurrent hosts we’re hitting. The one little trick here is using theusingscope modifier here so PS pulls in the variable defined in our main scope (gory details on scoping here
Click through for the script, and do check out the comments, where Stuart gives a bit of advice when you’re trying to execute against a large number of servers.