WMI Ping (Win32_PingStatus)
The code below is an example in how to use Win32_PingStatus class in WMI to check a remote machine's status on the network.
Code Snippet:
Function PingHost(sTarget)
Set cPingResults = GetObject("winmgmts:{impersonationLevel=impersonate}//" & _
sHost & "/root/cimv2"). ExecQuery("SELECT * FROM Win32_PingStatus " & _
"WHERE Address = '" + sTarget + "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
If LCase(sTarget) = oPingResult.ProtocolAddress Then
WScript.Echo sTarget & " is responding"
Else
WScript.Echo sTarget & "(" & oPingResult.ProtocolAddress & ") is responding"
End If
Wscript.Echo "Bytes = " & vbTab & oPingResult.BufferSize & _
vbTab & "Time (ms) = " & vbTab & oPingResult.ResponseTime & _
vbTab & "TTL (s) = " & vbTab & oPingResult.ResponseTimeToLive & _
vbTab & "Hostname = " & vbTab & oPingResult.ProtocolAddressResolved
Else
WScript.Echo sTarget & " is not responding"
WScript.Echo "Status code is " & oPingResult.StatusCode
WScript.Echo "*********************************"
End If
Next
End Function
If at first you fail, call it version 1.0
1 comment:
The "&" in Line 10 should be changed to "&".
Post a Comment