Saturday, March 22, 2014

PowerShell 002 - Test-Connection

Another common day-in-the-life type post. We have all been there. If we are systems engineers, developers, hobbyists, tinkers or the like we find ourselves troubleshooting network issues. And the first thing we do? Anyone? PING. Right? We all head to PING or IPConfig or some basic tool and we ‘PING’ something to begin figuring out why we can’t connect or why our connection is wonky. Yes ‘wonky’ is a real word and completely acceptable.

OK, but wait. PING? Isn’t that a command my Grandfather taught me? That is so old school. I’m modern, cool, wicked smaht (as we say in Boston), there has to be something better than PING. PathPing, Tracert etc. etc. not bad, interesting command line tools but still old-school. I want PowerShell, how can I do this in PowerShell?

Get-Help


I may try to use this as a bit of a theme. Introduce a little bit about the PowerShell help system in every post. It is so very cool and well worth the time. ‘Teach a man to fish’ and all that!
So the goal here is to navigate through and look at issue with your network connection. Open up the PowerShell ISE or Console, your choice (choose ISE) and enter;

PS C:\> get-help network

Hit Enter and you will get a list of far too many options. OK, time to narrow down. How about
;
PS C:\> get-help connection

Still too many results. VPNClient stuff, SMBShare stuff, Hyper-V, lots of interesting things but not what I want. Wait, I can use wildcards can’t I! Wildcards are your savior when trying to figure out things in PowerShell. Enter this;

PS C:\> get-help *connect*

The list is still too long but take a look at some of the info in the ‘synopsis’ column. A quick scan and you will see  “…ICMP echo… blah blah” that sounds an awful lot like PING. Let’s check it out. The cmdlet is called Test-Connection. Now go to the source, type the following and read through the output.

PS C:\> Get-Help Test-Connection -Full

Take a look at the –ComputerName parameter. The syntax of the parameter looks like this

-ComputerName <String[]>

The square brackets after String tell you that you can have multiple inputs, separated by a comma so this will work great!

PS C:\> Test-Connection -ComputerName srv1, file01


Test-Connection


One benefit to this cmdlet over PING is that it is machine independent. It can easily work with multiple machines at a time and provide heuristics about connections between two remote hosts. From the Help file you can see all of the available parameters, and since you used the –Full switch some practical examples of how to use the cmdlet are shown at the bottom.

Let’s keep this introduction to Test-Connection simple. We will only cover a few of the parameters, -ComputerName, -Count and –Source.

-ComputerName


This is a positional parameter and it is required. This means you must state what host you wish to test with (I almost said PING <g>), but if you place the computer name immediately after Test-Connection you do not need to specify the parameter name. For a best practice leave it in there. It makes reading PS info much easier later as you begin sharing with colleagues, friends, family and whoever is still listening to you J. These two statements are the same…

PS C:\> Test-Connection -ComputerName srv1
PS C:\> Test-Connection srv1

If you need to test the connection to multiple machines at a time simply separate the machines names with a comma.

PS C:\> Test-Connection -ComputerName srv1, file01 


-Count


This switch simply tells the cmdlet how many echo requests to send. Similar to PING it defaults to 4 but if you are looking at a connection and need to keep it going as you do something you can crank this up.

PS C:\> Test-Connection -ComputerName srv1 -Count 10


-Source


This one IMO is very powerful. This allows you to sit at your computer and very simply test a connection between two remote systems. Imagine you are troubleshooting an n tiered app and some performance issues. You can use this to quickly determine ICMP issues or network latency issues between any two nodes in your application design. Quickly finding where a slowness is occurring can help you narrow down where to dig deeper.

PS C:\> Test-Connection -ComputerName srv1, file01 -Count 5 -Source dc1

This cmdlet with PING srv1 and file01 5 times from dc1.

Source Destination IPV4Address    IPV6Address Bytes Time(ms)
------ ----------- -----------    ----------- ----- --------
DC1    file01      192.168.137.1              32    0      
DC1    srv1        192.168.137.11             32    0      
DC1    file01      192.168.137.1              32    0      
DC1    srv1        192.168.137.11             32    0      
DC1    file01      192.168.137.1              32    0      
DC1    srv1        192.168.137.11             32    0      


Look deeper into the help files. If you are into WMI and building really powerful scripts and automation, this cmdlet returns a WMI object called Win32_PingStatus object that you can use for some amazing things.

As always, Enjoy!

No comments:

Post a Comment