我有一个命令
commande = "cmd /c sc query state= all | findstr SERVICE_NAME | find /c /I "&LISTENER&"$"
我希望检索以后在我的脚本中处理的内容。
答案 0 :(得分:0)
您需要一个WshScriptExec
对象,因此您可以在完成后阅读其StdOut
属性:
Set sh = CreateObject("WScript.Shell")
Set cmd = sh.Exec("...")
Do While cmd.Status <> 1
WScript.Sleep 100
Loop
output = cmd.StdOut.ReadAll
答案 1 :(得分:0)
这是复杂脚本的改编版本(从original documentation by Microsoft MSDN中窃取)。它从执行的命令提示符窗口中抓取StdOut
和StdErr
个流:
option explicit
On Error GoTo 0
Dim strResult
myCmdExec "%comspec% /C dir /B /AD xxw*"
myCmdExec "%comspec% /C dir /B /AD fil*"
myCmdExec "cmd /c sc query state= all | findstr SERVICE_NAME | find /I /c ""pcasvc"""
myCmdExec "cmd /c sc query state= all | findstr SERVICE_NAME | findstr /I ler$"
Private Sub myCmdExec( commande)
strResult = commande & vbNewLine
Dim WshShell, oExec, xExitCode, input, allInput
allInput = ""
Set WshShell = CreateObject( "WScript.Shell")
Set oExec = WshShell.Exec( commande)
Do While True
input = ReadAllFromAny( oExec)
If input = -1 Then
If oExec.Status = 1 Then Exit Do
WScript.Sleep 100
Else
allInput = allInput & input
End If
Loop
xExitCode = oExec.ExitCode
If xExitCode <> 0 Then strResult = strResult & "Warning: Non-zero "
strResult = strResult & "exit code " & CStr( xExitCode) & vbNewLine _
& String( 30, "-") & vbNewLine
Wscript.Echo strResult & allInput & String( 30, "=") & vbNewLine
End Sub
Function ReadAllFromAny( oExec)
If Not oExec.StdOut.AtEndOfStream Then
ReadAllFromAny = oExec.StdOut.ReadAll
Exit Function
End If
If Not oExec.StdErr.AtEndOfStream Then
ReadAllFromAny = oExec.StdErr.ReadAll
Exit Function
End If
ReadAllFromAny = -1
End Function
<强>输出强>:
==>cscript D:\VB_scripts\SO\30320862.vbs
%comspec% /C dir /B /AD xxw*
Warning: Non-zero exit code 1
------------------------------
File Not Found
==============================
%comspec% /C dir /B /AD fil*
exit code 0
------------------------------
files
==============================
cmd /c sc query state= all | findstr SERVICE_NAME | find /I /c "pcasvc"
exit code 0
------------------------------
1
==============================
cmd /c sc query state= all | findstr SERVICE_NAME | findstr /I ler$
exit code 0
------------------------------
SERVICE_NAME: Spooler
SERVICE_NAME: TrustedInstaller
==============================
==>
答案 2 :(得分:0)
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Service")
For Each objItem in colItems
If Lcase(objitem.Name) = "audiosrv" Then
msgbox objitem.name & " " & objitem.status & " " & objitem.state
objitem.StartService
End If
Next
是做你想做的更好的方法。
<强>输出强>
---------------------------
---------------------------
Audiosrv OK Running
---------------------------
OK
---------------------------