Ezine 203 – Abandon Ping in Favor of Test-Connection

Test-Connection Measures Response Time for Website

Let us experiment with PowerShell’s Test-Connection and see how it’s more interesting and flexible than good old ping.

This Week’s Secret

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, switch to PowerShell’s equivalent called test-Connection.  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].

This Week’s Mission – Test-Connection Measures Response Time for Website

Getting Started: Switch from Ping to test-Connection

# PowerShell’s 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 script 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)
Write-host "Average response time to $Server is $Calc ms"

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!  See more on Test-Connection

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) 

WMI Monitor DetailsSolarwinds WMI Software

Here is another chance to download this free WMI Monitor.  It enables to you to measure the real-time performance of any modern Microsoft operating system.  Your FREE WMI Monitor will deliver a desktop dashboard that monitors any Windows server and offers built-in templates plus community customizable templates.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, Exchange mailboxes or SharePoint.

WMI Monitor Highlights:

  • Monitor real-time performance metrics on any Windows computer or application.
  • Leverage a large selection of pre-built and community generated application templates.
  • Modify or design your own application templates with the built-in WMI browser.
  • Use the WMI Monitor to familiarise yourself with classes that can be then used for PowerShell scripting.
  • Download Your Free WMI Monitor
  • See more on WMI Monitor

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary of Test-Connection

One reason for learning PowerShell that you can still use familiar command such as ipconfig /all and ping in the PowerShell GUI.  The benefits of test-Connection are two-fold, you can measure average response times, something that you could not do with ping, and you can use test-Connection as a vehicle for learning about PowerShell’s mathematical cmdlets such as measure-Object and [System:Math].

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

 


See more Microsoft PowerShell v 3.0

PowerShell 3.0  • What’s New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where