We setup new PsSessions using
New-PsSession, I set
SilentlyContinuejust 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 calling
Invoke-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 the
usingscope 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.