Network PING Function

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Network PING Function

Post by Marcus Tettmar » Sun Oct 28, 2007 2:26 pm

This VBScript function can be used to PING a network host:

Code: Select all

VBSTART
Function Ping(strHost)
  dim objPing, objRetStatus

  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
      'MsgBox "Status code is " & objRetStatus.StatusCode
    else
      Ping = True
      'MsgBox "Bytes = " & vbTab & objRetStatus.BufferSize & vbCRLF & _
      '       "Time (ms) = " & vbTab & objRetStatus.ResponseTime & vbCRLF & _
      '       "TTL (s) = " & vbTab & objRetStatus.ResponseTimeToLive
    end if
  next
End Function
VBEND

VBEval>Ping("microsoft.com"),pinged
If>pinged=True
  MessageModal>host contacted
Else
  MessageModal>host not contacted
Endif
The above version simply returnes True or False. But not the MsgBox lines that have been commented out which show how more detailed information can be retrieved.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts