-1

我询问有关获取此 cmd 命令 netstat -a |的结果的问题。查找 /c "TCP"

并使用 VBSCRIPT 将其与特定值进行比较

谢谢

4

1 回答 1

1

用这么少的细节很难回答问题,但无论如何让我们尝试......

下面的示例代码将说明两种将命令行输出存储到 vbs 中的变量的方法。WScript.Echo仅用作证据(显示结果)。

'** VAR#1 (using Exec & StdOut) ----------
Dim ObjExec
Dim strFromProc

Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c " _
    & "netstat -a | find /c " & Chr(34) & "TCP" & Chr(34))

Do Until ObjExec.Stdout.atEndOfStream
    strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
Loop

WScript.Echo strFromProc ' display result from variable strFromProc

Set objShell = Nothing
Set ObjExec = Nothing

'** VAR#2 (using Run) --------------------
Const cLogFile = "result.txt"

Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c netstat -a | find /c " _
    & Chr(34) & "TCP" & Chr(34) & ">" & cLogFile, 0, True

Dim oFile, Result
With CreateObject("Scripting.FileSystemObject")
    If .FileExists(cLogFile) Then
        Set oFile = .OpenTextFile(cLogFile)
        Result = oFile.ReadLine
        oFile.Close
        Set oFile = .GetFile(cLogFile)
        oFile.Delete
    End If
End With

WScript.Echo Result ' display result from variable Result

Set oFile = Nothing
Set objShell = Nothing
于 2013-01-24T19:00:35.810 回答