PowerShell Test-Connection Cmdlet

The benefits of Test-Connection are two-fold: you can measure average response times, something that’s not possible with ping.  And you can employ Test-Connection as a vehicle for learning about PowerShell’s mathematical cmdlets such as Measure-Object and [System:Math].

Topics for PowerShell Test-Connection Cmdlet

 ♣

Test-Connection Measures Response Time for Website

Ping has been the traditional command to test connectivity.  My mission is to show how PowerShell can achieve better control through its cmdlet Test-Connection.

Getting Started: Switch from Ping to Test-Connection

Assumption: You not only have installed and setup PowerShell, but also it’s PowerShell v 2.0 or later, and not v 1.0.

# PowerShell’s Test-Connection Cmdlet
# Equivalent of Ping
ping computerperformance.co.uk

# Now launch PowerShell and try:
Test-Connection computerperformance.co.uk

Benefits of Test-Connection Compared to Ping

This following script contains 3 elements:

  1.  Test-Connection (Ping)
  2.  Measure-Object to calculate the average.
  3.  A static method called System.Math, which we’ll use to round the number of milliseconds. 

There is no doubt that you could make a much more efficient script, however my version pays homage to divide and rule.  I believe that the easiest way to learn scripting is to break the task into chunks, then bolt all the bits together.  Furthermore, my version encourages you to make alterations, for example, change the value of $Server and the number of tests to ‘count’.

# PowerShell Test-Connection to measure a server's response time
Clear-host
$Avg = 0
$Server = "www.computerperformance.co.uk"
$PingServer = Test-Connection -count 3 $Server
$Avg = ($PingServer | Measure-Object ResponseTime -average)
$Calc = [System.Math]::Round($Avg.average)
"The average response time to $Server is $Calc ms `n"

Note 1:  Test-Connection is more flexible than Ping.  You can research more properties for your scripts by prefixing Get-Help to Test-Connection, also try suffixing Get-Member.

Note 2:  ResponseTime is a property of Test-Connection, the benefit of using this PowerShell cmdlet rather than ping is that we can calculate average connection times.

Note 3:  At first sight [System.Math]:: seems a strange way to call for mathematical functions such as Round, Truncate or log, however you soon get used to its ways!

Note 4:  See more on PowerShell’s Measure-Object

Experiment:  You cannot learn without making mistakes, try changing the parameters below, some will make no sense, while others will be predictable.  Only by experimenting will you truly understand how to handle PowerShell’s math functions.

ResponseTime -Sum (Also -Maximum, -Count)
Round($Avg.count) (Also .Maximum  .Count  .Sum)

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Using an Array to Hold the Computer List

Here is a technique for checking multiple host names.  Feel free to amend the list of sites in the array.  Why not choose computer names instead?

# PowerShell Array to hold names
Clear-Host
$Victim = @("www.computerperformance.co.uk","www.bbc.com","www.google.com")
Test-Connection $Victim

Note 5: For testing multiple sites, or multiple server names, consider creating an array to hold the values.

Research Parameters for Test-Connection

Let us research parameters using PowerShell's built-in help.  For example, thanks to -Source and -Computer you can specify both the sending and receiving computers.

Clear-Host
Get-Help Test-Connection -Full

Tip: Employ -AsJob to run the test in the background.

Note 6: This is how I found the -Count parameter, which improves this code.

# PowerShell Test-Connection Parameter -Count
Clear-Host
$Victim = @("www.computerperformance.co.uk","www.bbc.com","www.google.com")
Test-Connection $Victim -Count 1

Note: 7: See how muddled -Count 4 would be.  However, the most important point is that you always find a hand parameter when your precede a cmdlet with Get-Help.

See more tasks for PowerShell »

Summary of Test-Connection Cmdlet

One of the most attractive reasons for giving PowerShell a try is that you can still use familiar cmd.exe command such as ipconfig /all and ping in PowerShell.  Once started you, can build on existing knowledge and soon achieve higher levels of skill than were possible with cmd.exe. 

Taking ping as example, you can use the same switches that you used in DOS, but better still, changeover to PowerShell’s equivalent called Test-Connection.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials:

PowerShell Home   • Test-ServerHealth  • Test-SystemHealth   • Test-Connection  • Test-Path

PowerShell Logon Script  • PowerShell add printer  • PowerShell Schedule Task  • Free CSVDE Tool

Map Network Drive  • Exchange 2010 PowerShell Cmdlets   • Exchange 2010 Performance Monitor

Please email me if you have a better script examples. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.