Scripting. Powershell, VMware, Windows, Active Directory & Exchange. All that kind of stuff…..
RSS icon Email icon Home icon
  • PowerShell 2.0: One Cmdlet at a Time #6 Test-Connection

    Posted on November 19th, 2009 Jonathan Medd No comments

    Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Test-Connection cmdlet.

    What can I do with it?

    Send a ping to one or more computers

    Examples:

    Send a ping to Server01

    Test-Connection -ComputerName Server01

    If the result of a ping to Server01 is successful then copy a text file to a file share on that server

    If (Test-Connection -computername Server01 -quiet)
    {Copy-Item C:\Document.txt "\\Server01\Fileshare"}

    How could I have done this in PowerShell 1.0?

    You could have used Get-WMIObject with the Win32_PingStatus class.

    Get-WmiObject Win32_PingStatus -filter "Address='Server01'"

    Funnily enough Test-Connection and Get-WMIObject -class Win32_PingStatus look pretty similar when you pipe them through to Get-Member ;-)

    You could also have used the .NET System.Net.NetworkInformation.Ping class

    $ping = New-Object System.Net.NetworkInformation.Ping
    $ping.send('Server01')

    1000 things 1% better!

    Leave a reply