Wednesday, June 2, 2010

Things I learned from the Scripting Games 2010 #3: Test-Connection

Who knew there was a cmdlet designed just for testing remote connections?  Not me, at least until the scripting games this year.  For the past....forever, I've been doing something like the following to test remote connections

$pingResults = Ping-Host -HostName $ComputerName -Count 2 -Quiet -ErrorAction SilentlyContinue
if ($pingResults.received -gt 0)
{
    # Do something
}

And then I discovered test-connection! Now for simple quick tests we can just do:

if (Test-Connection $ComputerName -count 2 -quiet)
{
    #Do something
}

For larger groups of computers I'll still use PSCX's Ping-Host cmdlet due it's ability to ping all specified hosts asynchronously and it's speed.  You've updated to PSCX 2.0, right?  But Test-Connection is the right tool at the right price for quick connectivity checks.  Learn more about Test-Connection with Get-Help Test-Connection or on Technet.