VBS如何提取指定进程的PID?

2024-12-27 14:45:17
推荐回答(2个)
回答1:

修改后的代码如下:

Set w = GetObject("winmgmts:")
Set p = w.ExecQuery("select * from win32_process where name='1.exe' or name='2.exe' or name='3.exe' ")
if p.Count = 0 then
msgbox "指定进程未运行或用户权限不足以获得其信息。"
else
For Each i In p
msgbox "进程 " & i.name & " 的 PID 是 " & i.ProcessId
Next
end if

以上代码在我机器(win7)上测过没问题的,要是在你那里出错你可贴个截屏上来我试着看看。

------
修改前的:

'使用 WMI 对象

Set w = GetObject("winmgmts:")
Set p = w.ExecQuery("select * from win32_process where name= "& Chr(34) & "chrome.exe" & Chr(34) )
For Each i In p
msgbox "进程 " & i.name & " 的 PID 是 " & i.ProcessId
'i.TerminateProcess '用于杀死该进程
Next

例子是找所有 chrome.exe 的PID,你自己改改就是1、2、3.exe了

回答2:

dim Const wbemFlagReturnImmediately = &h10
Dim Const wbemFlagForwardOnly = &h20

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
MsgBox "ProcessId: " & objItem.ProcessId
Next