简体   繁体   中英

VBscript Ping Loop Script issue

I'm writing a VBscript that PINGS the server and loops.

If "Request Time Out" is more than two, then Wscript.echo"cannot connect to server" should be executed.

My code is below. There don't seem to be any syntax errors, but wscript.echo isn't being executed either! Can anyone help me solve this?

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
 Rem Sends email alert
 Set wshShell = WScript.CreateObject( "WScript.Shell" )

strComputer = "."

strCommand = "ping SER1"
strResults=""
while InStr(1,StrResults,"Request timed out")>2

    Set objExecObject = objShell.Exec(strCommand)
    Do While Not objExecObject.StdOut.AtEndOfStream
WScript.echo "Cannot Connect to Server"
        strResults = objExecObject.StdOut.ReadAll()


    Loop
wend

As JosefZ points out, your While loop never executes because the InStr condition is False when you first test it. What you need to do is turn the outside loop into a Do ... Loop While loop.

Another issue is you're putting Set objExecObject = objShell.Exec(strCommand) inside the loop. You presumably only want that to be executed once, then loop on the StdOut, so it should be put before the loops start.

Set objExecObject = objShell.Exec(strCommand)
Do
    Do While Not objExecObject.StdOut.AtEndOfStream
        WScript.echo "Cannot Connect to Server"
        strResults = objExecObject.StdOut.ReadAll()
    Loop
Loop While InStr(1,strResults,"Request timed out")>2

One thing I'm wondering about is " If "Request Time Out" is more than two... ". If you mean if that text exists at all (at a position greater than 2), then it will work; if you mean found more than twice, then it won't, because InStr simply returns the position of the searched for text in the string, not a match count.

Finally, it's not abundantly clear why you're using two loops. The first one effectively does nothing at all. The second one simply outputs the same text regardless of what really happened. This is what I think you want:

strCommand = "ping microsoft.com" ' microsoft.com times out for me
Set objExecObject = objShell.Exec(strCommand)
Do While Not objExecObject.StdOut.AtEndOfStream
    If InStr(1, objExecObject.StdOut.ReadLine, "request timed out", 1) <> 0 Then
        WScript.echo "Cannot Connect to Server"
        objExecObject.Terminate
        Exit Do
    End If
Loop

This will check the StdOut for "request timed out". If found then echo text, terminate ping, and exit loop. Note it doesn't check if it happened more than twice as I'm unsure whether that's what you wanted; if so then you probably should create some variable whose count increases on each iteration, and if it's twice/more than twice then do the same (echo, terminate, exit loop).

// edit

Ok so an "infinite" ping until request times out, at which point it should print the message and exit loop. This can be very easily accomplished in the ping command itself, with the -t switch - this will cause the ping to continue indefinitely rather than stopping after 4 pings as per default. So we just add it to this line: strCommand = "ping microsoft.com -t" ' microsoft.com times out for me and keep the rest of the code (the one I posted with the single loop) the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM