Stuart Moore shows an easy way to execute a Powershell script against multiple servers:
We setup new PsSessions using
New-PsSession
, I setErrorAction
toSilentlyContinue
just 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-Service
there’s no need to build a a new function, we can just call this directly here. By callingInvoke-Command
against 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-ThrottleLimit
parameter to limit the number of concurrent hosts we’re hitting. The one little trick here is using theusing
scope 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.