vbs中如何获取wmic命令的输出结果
答案:1 悬赏:30 手机版
解决时间 2021-04-07 22:11
- 提问者网友:相思似海深
- 2021-04-07 16:48
vbs中如何获取wmic命令的输出结果
最佳答案
- 五星知识达人网友:十年萤火照君眠
- 2021-04-07 17:13
有这个必有吗!?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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''代码结束'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯