Discoveries using PowerShell

PowerShell Remoting – Check Connectivity

by ps-gregg on October 6, 2013

PowerShell Remoting is a great tool that I use everyday, but there are many times when the I attempt to connect to a remote system and I receive a red error message. These errors are usually due to simple things like WSMAN service is stopped, a firewall rule is enabled, or the system is down. When I have a scheduled time that a set of tasks has to be completed by against thousands of machines, I simply don’t have the opportunity to stop and troubleshoot each error. So, I needed a way to test PowerShell Remoting connectivity to check whether the remote systems are responding to connection requests. I’ve come up with a simple and effective way to check the connectivity and provide a list of systems that have succeeded and failed.

The first step is to create an array of computer names that I want to check. For this article I will hard code the list, but I encourage you to use your favorite method – retrieve from a text file, retrieve from Active Directory, or whatever method works for your situation.

Next I use the Invoke-Command cmdlet which will attempt to connect to each computer name in the array and run a command. I usually run the Get-Culture cmdlet (returns language and locale information) to test whether I have successfully connected to the remote workstation. I pipe the results into Select-Object and retrieve the content of the PSComputerName property. The content will be the computer name of the successful connection and will be a string value which I will store in an array with all the other computers that succeed.

Then I use the Compare-Object cmdlet to compare the original array of computers with the array containing the computers that succeeded. The output of the Compare-Object cmdlet is piped into Select-Object to retrieve the contents of the InputObject property. The content will be the computer names of the failed connections and each will be a string value which I will store in an array to show which computers failed.

Here is the complete code:

Now I can run whatever actions I want using the $succeeded array and if I’m lucky, I’ll be able to hand the $failed list off to someone else to fix the PowerShell Remoting connectivity.

Come on – I can dream, can’t I ?