Reputation: 92
I am looking for a way to ping a hostname via a VBScript and if it fails after some bad counts, if offline, it runs another vbs file.
I have many scripts that can ping a host via VBScript, but I don't know and I couldn't find a way to ping a host and if not pinging, to redirect and run another VBScript.
I've tried to add in the end of one of those scripts something like:
If strFailedPings = "" Then WScript.Echo "Ping status of specified computers is OK" Else
(here is the place that goes the code to open another vbs)
Example:
strComputers = "192.168.1.1,192.168.1.4"
arrComputers = Split(strComputers, ",")
For Each strComputer In arrComputers
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("SELECT * from Win32_PingStatus WHERE address = '" & _
strComputer & "'")
For Each objPingStatus In objPing
If IsNull(objPingStatus.StatusCode) Or objPingStatus.StatusCode<>0 Then
If strFailedPings <> "" Then strFailedPings = strFailedPings & vbCrLf
strFailedPings = strFailedPings & strComputer
End If
Next
Next
If strFailedPings = "" Then
WScript.Echo "Ping status of specified computers is OK"
Else
WScript.Echo "Ping failed for the following computers:" & _
vbCrLf & vbCrLf & strFailedPings
End If
Upvotes: 1
Views: 776
Reputation: 5031
I don't think you can break up the GetObject line:
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * from Win32_PingStatus WHERE address = '" & strComputer & "'")
When I run the script (I replaced the Echo with MsgBox but it shouldn't matter) it works fine:
strComputers = "192.168.1.1,192.168.1.4,100.100.100.100"
arrComputers = Split(strComputers, ",")
For Each strComputer In arrComputers
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("SELECT * from Win32_PingStatus WHERE address = '" & strComputer & "'")
For Each objPingStatus In objPing
If IsNull(objPingStatus.StatusCode) Or objPingStatus.StatusCode <> 0 Then
If strFailedPings <> "" Then strFailedPings = strFailedPings & vbCrLf
strFailedPings = strFailedPings & strComputer
End If
Next
Next
If strFailedPings = "" Then
MsgBox "Ping status of specified computers is OK"
Else
MsgBox "Ping failed for the following computers:" & _
vbCrLf & vbCrLf & strFailedPings
End If
Upvotes: 0
Reputation: 200233
Run the WMI query n times in a For
loop and count and count the failed pings, then run the other script if the fail count exceeds a given threshold.
n = 3
threshold = 1
Set wmi = GetObject("winmgmts://./root/cimv2")
For Each strComputer In arrComputers
qry = "SELECT * FROM Win32_PingStatus WHERE address='" & strComputer & "'"
cnt = 0
For i = 1 To n
For Each objPingStatus In wmi.ExecQuery(qry)
If IsNull(objPingStatus.StatusCode) Or objPingStatus.StatusCode<>0 Then
cnt = cnt + 1
End If
Next
Next
If cnt > threshold Then
'ping failed 2 or more times
CreateObject("WScript.Shell").Run "wscript.exe ""C:\other\script.vbs""", 0, True
End If
Next
Side note: you may want to distinguish between sNull(objPingStatus.StatusCode)
and objPingStatus.StatusCode<>0
. Only the latter represents the actual ping status. The former indicates a WMI error.
Upvotes: 1