Programming Windows

VBScript: Ping Function

Just another useful function using the Windows Management Infractstructre (WMI) for doing a ICMP Ping. I’ve used this function on all my network related scripts to verify a host is available on the network.

' #####################################################################
' ##
' ## (C) 2009 Michael Miklis (michaelmiklis.de)
' ##
' ## 
' ## Filename:      Ping.vbs
' ##
' ## Version:	    1.0
' ##
' ## Release:	    Final 
' ##
' ## Requirements:  -none-
' ## 
' ## Description:   Pings a network host or device
' ##
' ## This script is provided 'AS-IS'.  The author does not provide 
' ## any guarantee or warranty, stated or implied.  Use at your own 
' ## risk. You are free to reproduce, copy & modify the code, but 
' ## please give the author credit. 
' ##
' ####################################################################
Option Explicit

If Ping("www.google.com") = true then
	wscript.echo "alive"
End If

'______________________FUNCTIONS & SUB ROUTINES_______________________

Function Ping(strHost)
	'// <summary> 
	'// Well Known Ping command
	'// </summary>
	'// <param name="strHost">Name or IP-Address</param>


	Dim objPing			'WMI Ping Object
	Dim objStatus			'PingStatus Object

	Ping = false

	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
	ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strHost & "'")

	'// Query Win32_PingStatus.StatusCode -> 0 = reachable
	For each objStatus in objPing
		If objStatus.StatusCode = 0 then
			Ping = true
			Exit Function
		End If
	Next
End Function
You may also like
Clear all Windows Evenlogs
Creating VMware Virtual Machines using PowerCLI
Get running processes including CPU and memory usage

Leave Your Comment

Your Comment*

Your Name*
Your Webpage