header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to work with the Ping command in the Powershell?
Last Post 13 Aug 2010 10:25 PM by angeldeepa. 13 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Resolved
TalUser is Offline
New Member
New Member
Posts:43
Avatar

--
28 Dec 2009 12:59 AM

    i want to check if the if have Ping to the server

    i made this script:
    $Srvname = "server.com"
    $ping = new-object System.Net.NetworkInformation.Ping
       
          if ( $Ping.Send("$srvname".status -eq "Success")
                {
                   echo " Success"
                }
                else
                {
                   echo " Failed"
                }

    The problem is that if the server Does NOT Exist i get an expection  error and the script does not continue

    how can i continue if the server does not exist and it should go also to the FAILED

    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    28 Dec 2009 01:41 AM
    The correct script would be as below.. Slight change

    $Srvname = "server.com"
    $ping = new-object System.Net.NetworkInformation.Ping

    $test = $ping.send($server)

    if ( (!$?)
    {
    Write-host "Couldn't find the IP address of the HOST mentioned"
    }
    elseif ($test.status -eq "Success")
    {
    Write-host "Success"
    }
    Else
    {
    Write-Host "Failed with status..."
    $test.status
    }

    The exception error is occurred when the name does not exist and it is not being resolved to IP address.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    TalUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    28 Dec 2009 06:39 AM
    The row $test = $ping.send($server)
    this was before the problem also
    when the script gets to it gives an error of
    Execption calling "send" with "1" arguments : " Value cannot be null
    parameter name : address
    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    28 Dec 2009 06:58 AM
    Sorry! the variable should $Srvname instead of $server.

    ...
    $test = $ping.send($Srvname)
    ...

    The condition in first if statement will verify if the above command is successfull or not. The Condition says, if the above command is NOT successful (i.e. gives error - it means the given HOST name is resolve to IP) then execute the statement (Write-host "Couldn't find the IP address of the HOST mentioned" ).

    Thanks.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    TalUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    28 Dec 2009 07:51 AM
    i understand the script
    but still in the row where with $test
    is still gives me an expection and it does not work to put the value of the $ping.send($srvname) into the $test parameter
    it breaks out when it is trying to put in some value. it is not getting to the phase of IF

    Vishal RamnaniUser is Offline
    New Member
    New Member
    Posts:68
    Avatar

    --
    28 Dec 2009 11:53 PM
    ohh!! Apologies.

    Please remove "(" also from if condition. Try below. i tested it.

    #start
    $Srvname = Read-Host "enter server name or IP address"
    $ping = new-object System.Net.NetworkInformation.Ping

    $shell = New-Object -ComObject wscript.shell
    $test = $ping.send($Srvname)

    if (!$?)
    {
    $shell.popup("Couldn't find the IP address of the HOST mentioned")
    }
    elseif ($test.status -eq "Success")
    {
    $shell.popup("Success")
    }
    Else
    {
    $status = $test.status
    $shell.popup("Failed with status: $status")
    }
    #end

    You can replace shell pop with your required command if you don't want shell pop up.
    Thanks.
    Vishal Ramnani
    MCITP - Exchange 2007, MCSE Messaging, MCTS - Win 2008 Config
    johndauphineUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    29 Dec 2009 04:27 AM
    Here is a quick and dirty powershell script to check the status of a list of servers. http://bit.ly/8M7ZBQ This may help with the ping object exception issue.
    TalUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    29 Dec 2009 04:36 AM
    for vishalramnani thanks alot but still i get the same error with Expetion maybe you load somthing in the script before you past the script you gave me ?



    for johndauphine :
    the script is ok without expetion but it gives me the same answer with and ping or without
    johndauphineUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    29 Dec 2009 04:48 AM
    • Accepted Answer
    I modified the script since I posted. The following line will catch unresolved DNS names: $r = ping $s|select-string "100% loss|could not find host"
    Lance RobinsonUser is Offline
    New Member
    New Member
    Posts:13
    Avatar

    --
    29 Dec 2009 01:05 PM
    Shatztal,

    You can use the try/catch in PowerShell 2. Read here:
    get-help try | more

    Very functional.


    Lance Robinson
    http://www.lancerobinson.net
    http://www.twitter.com/lmrobins
    TalUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    30 Dec 2009 05:05 AM
    For John:
    The script works ok but now after i am putting in server names it gives me this messege:

    |The script failed due to call depth overflow. The call depth reached 1001 and the maximum is 1000.

    MikeUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    30 Dec 2009 02:19 PM
    How would one run multiple instances of pathping at the same time?

    Thanks
    TalUser is Offline
    New Member
    New Member
    Posts:43
    Avatar

    --
    02 Jan 2010 10:26 PM
    thanks very much this helped me out good
    angeldeepaUser is Offline
    New Member
    New Member
    Posts:1
    Avatar

    --
    13 Aug 2010 10:25 PM
    Thanks friend..I usually perform the ping test here [url]http://www.whoisxy.com/ping.aspx[/url] ..
    Is it possible to ping with specific port?
    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