Tuesday, January 4, 2011

Using Test-Connection

'Test-Connection' is Powershell V2's GWMI-based icmp test cmdlet that returns considerable amounts of information in object format.  'Test-Connection' also has the ability to authenticate (at various levels) to the computer whose ICMP responses it is testing, but I do not discuss that in this post. 'Test-Connection' can return a number of errors, which I found difficult to trap, throw, or try-catch-finally.  So, like some others (1,2), I punted on error-trapping with:
 [string] $ErrorActionPreference="silentlycontinue"  (Yea, I know...what a wimp...) 
Although, 'Test-Connection' is slower than System.Net.NetworkInformation.Ping will probably ever be, it does return considerable amounts of useful information. Below is the function 'Ping-IP' with the returned object and some statistical results from 'measure-object'. I've created a hash table and renamed six of the properties.  Notice how I can use 'Test-Connection' to check for IPv4 and IPv6 connections simultaneously (if you have an existing IPv6 interface).


function global:Ping-IP 
{
   [CmdletBinding()]
   Param(
       [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
       [string]$computername,
       [int32] $buffersize=8,
       [int32] $count=1,
       [string] $ErrorActionPreference="silentlycontinue"
   )
$global:result= Test-connection -computername $computername -buffersize $buffersize -count $count

$global:ICMP_out = New-Object PSObject -Property @{
       IPv4          = $result.IPv4Address.IPAddressToString
       IPv6      = $result.IPv6Address.IPAddressToString
       BytesSent     = $result.BufferSize
       BytesReturned = $result.ReplySize
       ResponseTime  = $result.ResponseTime
       ReplyInc  = $result.ReplyInconsistency
  } | Select-Object IPv4,IPv6,BytesSent,BytesReturned,Responsetime,ReplyInc
if ($result -ne $null) {$ICMP_out}
}

First I create a partial subnet range to ping, pipe the function output ("ping-ip") to a variable and then sort the output by response time:

$IPRange=0..50 | %{"74.125.127.$_"}
$result=$IPRange | % {Ping-IP $_}
$result | Sort Responsetime -Descending | ft

IPv4              IPv6                      BytesSent     BytesReturned     ResponseTime         ReplyInc
----              ----                      ---------     -------------     ------------         --------
74.125.127.19                                       8                18              115             True
74.125.127.40                                       8                18               80             True
74.125.127.46                                       8                18               80             True
74.125.127.50                                       8                18               74             True
74.125.127.16                                       8                18               68             True
74.125.127.48                                       8                18               67             True
74.125.127.27                                       8                18               66             True
74.125.127.14                                       8                18               61             True
74.125.127.33                                       8                18               43             True
74.125.127.39                                       8                18               40             True
74.125.127.41                                       8                18               34             True
74.125.127.18                                       8                18               31             True
.....

Next, I can easily measure the "Responsetime" of my 'Test-Connection' queries with 'measure-object' :


$result | Measure-Object -Average -Maximum -Minimum -Sum -Property Responsetime

Count    : 28
Average  : 40.7857142857143
Sum      : 1142
Maximum  : 115
Minimum  : 19
Property : ResponseTime

There are considerable more statistics and graphing output that could be done with this function with log-parser, and/or a Powershell statistics package and/or csv exports to spreadsheet software. Creating a more robust function will involve more sophisticated error-trapping and or the use of the background jobs. To be continued...

No comments: