0

每次登录我的工作计算机时,我想使用 VB 脚本(Pastebin中的完整脚本)来检查某些服务器是否已启动(响应 ping)。

但是,似乎我正在获取输出的全部结果......

strPingResultsFull = LCase(WshExec.StdOut.ReadAll)

...在我遍历各个行之前...

Do Until WshExec.StdOut.AtEndOfStream

...那WshExec.StdOut已经是AtEndOfStream,所以我永远不会得到任何结果。

我探索的第一部分——这个理论是否正确(或可能正确)?第二部分 - 我如何解决这个问题(即重置流以便我可以单独检查每一行)?谢谢。

' Set the targets to ping
strTarget(0) = "192.168.1.6"    ' TTSA
strTarget(1) = "192.168.1.7"    ' TTSB
strTarget(2) = "192.168.1.14"   ' TTSC
strTarget(3) = "192.168.1.12"   ' TTSD

Set WshShell = WScript.CreateObject("WScript.Shell")

' Loop through all of the targets to ping
For i = 0 To UBound(strTarget)

    ' Ping the target
    Set WshExec = WshShell.Exec("ping -n 2 -w 500 " & strTarget(i)) ' Send 2 echo requests, waiting 0.5 seconds maximum

    ' Grab the full results of the ping
    strPingResultsFull = LCase(WshExec.StdOut.ReadAll)

    ' Set 'strPingResultsPart' to an empty string
    strPingResultsPart = ""

    ' Grab the results of the ping
    Do Until WshExec.StdOut.AtEndOfStream

        strSingleLine = WshExec.StdOut.ReadLine()
        If Instr(strSingleLine, "Ping statistics for") <> 0 Or Instr(strSingleLine, "Packets:") <> 0 Then
            strPingResultsPart = strPingResultsPart & strSingleLine
        End If

    Loop

    ' Check that there is something to output (there should be, even if the pings all fail)
    If strPingResultsPart <> "" Then

        Msgbox "Not empty - " & strPingResultsPart
        ' Add the ping results to the correct results strings
        If InStr(strPingResultsFull, "reply from") Then     
            strOutputSuccess = strOutputSuccess & strPingResultsPart & VBCrLf
        Else
            strOutputFailure = strOutputFailure & strPingResultsPart & VBCrLf
        End If

    End If

    Set WshExec = Nothing

Next
4

1 回答 1

2

在第一次打电话后WshExec.StdOut.ReadAll,你已经在AtEndOfStream……

Do Until WshExec.StdOut.AtEndOfStream

……什么都不做。您不能StdOut多次回过头来阅读它。

于 2013-04-03T11:18:01.003 回答