vbs中如何获取wmic命令的输出结果
1个回答
展开全部
有这个必有吗!?wmic能做到的vbs也能做到
----------vbs 获取Wmic Process Get ProcessId,ExecutablePath执行结果代码开始-----------------
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("%comspec% /c Wmic Process Get ProcessId,ExecutablePath")
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
Dim allInput, tryCount
allInput = ""
tryCount = 0
Do While True
Dim input
input = ReadAllFromAny(oExec)
If -1 = input Then
If tryCount > 10 And oExec.Status = 1 Then
Exit Do
End If
tryCount = tryCount + 1
WScript.Sleep 100
Else
allInput = allInput & input
tryCount = 0
End If
Loop
If oExec.ExitCode <> 0 Then
WScript.Echo "警告:CMD返回值非0"
End If
WScript.Echo allInput
---------------------------------------------------代码结束-----------------------------------------------------
而用vbs Wmi获取进程代码很简洁
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''vbs Wmi获取进程代码开始''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Wmi
Set Wmi = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set A = Wmi.Execquery("Select * From Win32_Process")
For Each B In A
Wscript.Echo B.ExecutablePath &vbTab& B.ProcessId
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''代码结束'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
----------vbs 获取Wmic Process Get ProcessId,ExecutablePath执行结果代码开始-----------------
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("%comspec% /c Wmic Process Get ProcessId,ExecutablePath")
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
Dim allInput, tryCount
allInput = ""
tryCount = 0
Do While True
Dim input
input = ReadAllFromAny(oExec)
If -1 = input Then
If tryCount > 10 And oExec.Status = 1 Then
Exit Do
End If
tryCount = tryCount + 1
WScript.Sleep 100
Else
allInput = allInput & input
tryCount = 0
End If
Loop
If oExec.ExitCode <> 0 Then
WScript.Echo "警告:CMD返回值非0"
End If
WScript.Echo allInput
---------------------------------------------------代码结束-----------------------------------------------------
而用vbs Wmi获取进程代码很简洁
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''vbs Wmi获取进程代码开始''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Wmi
Set Wmi = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set A = Wmi.Execquery("Select * From Win32_Process")
For Each B In A
Wscript.Echo B.ExecutablePath &vbTab& B.ProcessId
Next
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''代码结束'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询