简体   繁体   English

VBscript Ping循环脚本问题

[英]VBscript Ping Loop Script issue

I'm writing a VBscript that PINGS the server and loops. 我正在写一个PINGS服务器并循环的VBscript。

If "Request Time Out" is more than two, then Wscript.echo"cannot connect to server" should be executed. 如果"Request Time Out"大于两个,则应执行Wscript.echo"cannot connect to server"

My code is below. 我的代码如下。 There don't seem to be any syntax errors, but wscript.echo isn't being executed either! 似乎没有任何语法错误,但是也不执行wscript.echo! 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. 正如JosefZ指出的那样,您的While循环永远不会执行,因为首次测试InStr条件为False时。 What you need to do is turn the outside loop into a Do ... Loop While loop. 您需要做的是将外部循环转换为Do ... Loop While循环。

Another issue is you're putting Set objExecObject = objShell.Exec(strCommand) inside the loop. 另一个问题是您将Set objExecObject = objShell.Exec(strCommand)放入循环内。 You presumably only want that to be executed once, then loop on the StdOut, so it should be put before the loops start. 您大概只希望执行一次,然后在StdOut上循环,因此应将其放在循环开始之前。

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; 如果您是说该文本是否全部存在(位置大于2),那么它将起作用。 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. 如果您的意思是发现两次以上,则不会,因为InStr只是返回搜索文本在字符串中的位置,而不是匹配计数。

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". 这将检查StdOut是否存在“请求超时”。 If found then echo text, terminate ping, and exit loop. 如果找到,则回显文本,终止ping并退出循环。 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. 好的,这样就可以进行“无限”的ping操作,直到请求超时为止,此时应打印消息并退出循环。 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. 使用-t开关可以很容易地在ping命令本身中完成此操作-这将导致ping操作无限期继续,而不是默认情况下在进行4次ping之后停止。 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. 因此,我们将其添加到此行: strCommand = "ping microsoft.com -t" ' microsoft.com times out for me并保持其余代码(我在单循环中发布的代码)相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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