skip navigation

www.Hilands.com


Content:: VBS - Ping Function

VBS - Ping

You can add the function to your code and use it with a simple if statement
if Ping(strIP) = True then
	'ping was successful continue with code
else
	'unable to ping stop here or write error etc
end if
View the function below
'###############################################################################
'# Ping Function                                                               #
'#   takes input of IP or host name.                                           #
'#   returns true or false                                                     #
'#   if Ping(strIP) = True then                                                #
'#   http://windowsitpro.com/article/articleid/48449/how-can-i-use-a-vbscript-script-to-ping-a-machine.html
'###############################################################################
Function Ping(strHost)
	dim refPing, refRetStatus
	set refPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
		("select * from Win32_PingStatus where address = '" & strHost & "'")
	for each refRetStatus in refPing
		if IsNull(refRetStatus.StatusCode) or refRetStatus.StatusCode<>0 then
			Ping = False
			'WScript.Echo "Status code is " & refRetStatus.StatusCode
		else
			Ping = True
			'Wscript.Echo "Bytes = " & vbTab & refRetStatus.BufferSize
			'Wscript.Echo "Time (ms) = " & vbTab & refRetStatus.ResponseTime
			'Wscript.Echo "TTL (s) = " & vbTab & refRetStatus.ResponseTimeToLive
		end if
	next
End Function
ping.func.inc.zip
download Download 610 bytes
MD5 - 716f9b96284af1e455e1378a4b09aa94 *ping.func.inc.zip
Last Modified: 2012-10-21