vbs中如何获取wmic命令的输出结果

 我来答
fastslz
2012-06-13 · TA获得超过938个赞
知道小有建树答主
回答量:561
采纳率:100%
帮助的人:911万
展开全部
有这个必有吗!?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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''代码结束'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式