Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Repeat a PING (just once)

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
Here's my PING function (mostly ripped from somewhere else, I admit)

The first IF loop creates a false negative by appending the host name on first call of the function. Once a PING fails the function calls itself to try once more (the idea being to cancel out the possibility of a network blip) so the IF loop resets the strHost variable back to the original, correct, hostname during the testing.

Code:
Function Ping(strHost)
    PingCount = PingCount + 1
    if PingCount = 1 then
        t_strHost = strHost
        strHost = strHost & "1"
    Else
        strHost = t_strHost
        PingCount = 0
    end if

    wscript.echo " pinging " & strHost

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
            Ping = False
            if PingCount = 1 then
                Ping(t_strHost) ' Try again, just in case of a network 'blip'
            end if
        else
            Ping = True
        end if
    next
End Function

Problem is even after calling itself with the correct strHostname it's still reporting a PING failure.

What am I doing wrong?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Why not simply something like this ?
Code:
Function Ping(strHost)
For PingCount = 1 To 2
  WScript.Echo " pinging " & strHost
  Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
   ("select * from Win32_PingStatus where address = '" & strHost & "'")
  For each objRetStatus in objPing
    If objRetStatus.StatusCode = 0 Then
      Ping = True
      Exit Function
    End If
  Next
Next
Ping = False
End Function

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Increasing the timeout might help too.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top