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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I ping or resolve to an IP address within a VBScript?

WMI

How do I ping or resolve to an IP address within a VBScript?

by  dm4ever  Posted    (Edited  )
So I've seen a few requests lately concerning the ability to ping to check for connectivity within a vbscript. Most implementations I've seen call ping.exe using the exec method of wscript.shell and read the output via stdout. While this works just fine, I think using WMI to ping is cleaner and easier to work with. There is no need to read the output and try to extract what you want from a string like you do when using ping.exe. I hope some find this useful.

The [color red]Reachable[/color] function will return [color blue]True or False[/color] depending on whether the address provided is reachable or not. The address can be provided with an IP address or Computer Name
The [color red]ResolveIP[/color] function will return an [color blue]IPAddress[/color] that corresponds to the Computer Name you provide

I put together a simple [color red]"Connectivity Monitor"[/color] using Win32_PingStatus. You can download it from here (I put it in a zip because of the images used for it): [link http://dm4ever1.googlepages.com/ConnectivityMonitor.zip]ConnectivityMonitor[/link]

Reference for this class: http://msdn2.microsoft.com/en-us/library/aa394350.aspx
NOTE: WMI is only required on the LOCAL computer and NOT the destination computer.

[color red]Reachable Function[/color]
Code:
'==========================================================================
' The following function will test if a machine is reachable via a ping
' using WMI and the Win32_PingStatus
'==========================================================================
If Reachable("10.50.138.48") Then
	WScript.Echo "Computer is Reachable"
Else 
	WScript.Echo "Computer is Unreachable!"
End If

Function Reachable(strComputer)
' 	On Error Resume Next

	Dim wmiQuery, objWMIService, objPing, objStatus
	
	wmiQuery = "Select * From Win32_PingStatus Where " & _
	"Address = '" & strComputer & "'"
	
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objPing = objWMIService.ExecQuery(wmiQuery)
	
	For Each objStatus in objPing
		If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
			Reachable = False 'if computer is unreacable, return false
		Else
			Reachable = True 'if computer is reachable, return true
		End If
	Next
End Function

[color red]ResolveIP Function[/color]
Code:
'==========================================================================
' The following function will resolve a computer name to its ip address
' using WMI and the Win32_PingStatus
'==========================================================================
WScript.Echo ResolveIP("Computer1")

Function ResolveIP(strComputer)
' 	On Error Resume Next

	Dim wmiQuery, objWMIService, objPing, objStatus
	
	wmiQuery = "Select * From Win32_PingStatus Where " & _
	"Address = '" & strComputer & "'"
	
	Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
	Set objPing = objWMIService.ExecQuery(wmiQuery)
	
	For Each objStatus in objPing
		If IsNull(objStatus.StatusCode) Or objStatus.Statuscode<>0 Then
			ResolveIP = "Computer is Unreachable!"
		Else
			ResolveIP = objStatus.ProtocolAddress
		End If
	Next
End Function
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top