Wednesday, April 29, 2015

PowerShell and testing connectivity with Test-NetConnection

I have been working in IT long enough to see people stick to their old habits begrudgingly, and I had the realization today that one of the ones I was still holding on to was an inherent need for telnet.exe as a troubleshooting tool.

Let's face it, it is compact, a low security risk to have installed, but it having not been included by default on modern Operating Systems has forced me to memorize import-servermanager and add-windowsfeature telnet-client (and before that servermanagercmd -i telnet-client)

The fact that I can iterate those without looking them up is practically a sign that the tool might need a refresh.

Enter Test-NetConnection.   Now, I won't claim to have discovered or wrote it, but I use PowerShell daily and just learned of it today.

It's new since Windows 8.1 and Windows 2012 R2, which came with PowerShell 4.0.

Like I said - nothing new - it's already been called the SuperPing once.

So what can we do with it?

First off, run it alone is a basic Internet connectivity test using just test-netconnection.  This reaches out to a server at Microsoft to see if you are online.  Great to use programatically like this:

if (test-netconnection) { 
    # Download stuff
    } else { 
    # Error about your Internet

You can also use it to check specific connectivity using something like:
Test-NetConnection -ComputerName -Port 5061 -InformationLevel Detailed -Verbose

Some additional tips:
  • -ComputerName can be shortened to -CN
  • -commonport can be used for things like RDP, HTTP, SMB and PING
  • -traceroute can also be used to display hops to a destination, and -hops can be used to specify the maximum hops to be displayed

 As you can see, this gives us a BUNCH of great information:
  1. Multiple DNS records returned (only shown with the Detailed InformationLevel)
  2. Which IP we connected to
  3. And a successful TCP test!
Have to admit, I might be skipping nslookup and telnet in future deployments, and I hope you do too.   If only it could do UDP tests!

No comments: