header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
ping comand with error handling
Last Post 29 Jun 2010 01:20 AM by George Howarth. 8 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
YogeshUser is Offline
New Member
New Member
Posts:30
Avatar

--
25 Jun 2010 06:37 AM
    HI Friends,

    I was trying to build a script to ping a set of computer but it fails whenever we have a computername in the list which doesnot not exist. Script is working fine otherwise :

    $ping = new-object System.Net.NetworkInformation.Ping
    $p=$ping.send("computername")

    Now if computername exist then above command runs fine and give result as success but is "computername" doesnot exist then it thros the errors like :

    Exception calling "Send" with "1" argument(s): "An exception occurred during a Ping request."
    At line:1 char:14
    + $p=$ping.send <<<< ("dc1.test.com")
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException




    Now to add error handling I was trying :

    $p=$ping.send("computername")  -ea silentlycontinue -ev +err but it fails with :




    You must provide a value expression on the right-hand side of the '-' operator.
    At line:1 char:31

    Unexpected token 'ea' in expression or statement.
    At line:1 char:32

    Unexpected token 'silentlycontinue' in expression or statement.
    At line:1 char:35

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    25 Jun 2010 07:05 AM
    You can use Test-Connection with the -Quiet switch:

    if (Test-Connection -ComputerName "computer_name" -Quiet)
    {
    "computer responded"
    }
    else
    {
    "computer did not respond"
    }
    YogeshUser is Offline
    New Member
    New Member
    Posts:30
    Avatar

    --
    25 Jun 2010 09:55 AM
    Hi,

    But again if due to some reason if computer_name doesnot exis, it will throw the error.

    Regards
    Yogesh
    Bartek BielawskiUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    25 Jun 2010 10:15 AM
    You can add proper ErrorAction to prevent errors:
    if (Test-Connection -Quiet -Count 1 -ComputerName NonExistingHost -ErrorAction SilentlyContinue) {            
        'Host is there'            
    } else {            
        'Host is missing'            
    }
    HTH Bartek
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    25 Jun 2010 11:24 AM

    This may still throw an exception, but it will be caught in the catch block where it will write the type of the exception to the console:

    $ErrorActionPreference = "Stop"

    $ping = New-Object System.Net.NetworkInformation.Ping

    try
    {
        $pingReply = $ping.Send("computername")
    }
    catch [System.Net.NetworkInformation.PingException]
    {
        $_.Exception.InnerException.GetType().ToString()
    }

    switch ($pingReply.Status)
    {
        Success { }
        DestinationNetworkUnreachable { }
        DestinationHostUnreachable { }
        DestinationProtocolUnreachable { }
        DestinationPortUnreachable { }
        DestinationProhibited { }
        NoResources { }
        BadOption { }
        HardwareError { }
        PacketTooBig { }
        TimedOut { }
        BadRoute { }
        TtlExpired { }
        TtlReassemblyTimeExceeded { }
        ParameterProblem { }
        SourceQuench { }
        BadDestination { }
        DestinationUnreachable { }
        TimeExceeded { }
        BadHeader { }
        UnrecognizedNextHeader { }
        IcmpError { }
        DestinationScopeMismatch { }
        Unknown { }
        default { }
    }


    Details of the enumeration values can be found here: http://msdn.microsoft.com/en-us/lib...tatus.aspx

    Shay LevyUser is Offline
    PowerShell MVP, Admin
    Veteran Member
    Veteran Member
    Posts:1362
    Avatar

    --
    26 Jun 2010 06:11 AM
    when you specify the -Quiet switch test-connection returns a boolean result even if the target doesn't exist, no need to specify ErrorAction.


    PS > Test-Connection -ComputerName IDontExist -Quiet -Count 1
    False

    Shay Levy
    Windows PowerShell MVP
    http://PowerShay.com
    PowerShell Community Toolbar
    Twitter: @ShayLevy
    YogeshUser is Offline
    New Member
    New Member
    Posts:30
    Avatar

    --
    28 Jun 2010 09:59 PM
    Hi Shay, Thanks this is most convient way to do.


    YogeshUser is Offline
    New Member
    New Member
    Posts:30
    Avatar

    --
    28 Jun 2010 10:01 PM
    Hi GWHowarth88 ,

    I tried your script but $pingreply.status didnt threw any value ??

    Regards
    Yogesh Malhotra
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    29 Jun 2010 01:20 AM
    Works fine for me:

    $ping = New-Object System.Net.NetworkInformation.Ping
    $pingReply = $ping.Send("computername")
    $pingReply.Status
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer