【发布时间】:2017-08-10 05:23:15
【问题描述】:
使用 CMD 或 VBScript 从给定描述中获取进程 ID 或图像名称的最简单方法是什么?
Description = "My application*",我想获取所有具有该描述的进程 ID。
【问题讨论】:
使用 CMD 或 VBScript 从给定描述中获取进程 ID 或图像名称的最简单方法是什么?
Description = "My application*",我想获取所有具有该描述的进程 ID。
【问题讨论】:
枚举进程的最佳方法是 WMI。然而,不幸的是,Win32_Process 类的Description 属性只存储可执行文件的名称,而不是任务管理器在其“描述”字段中显示的信息。该信息是从可执行文件的extended attributes 中检索到的。
你可以用 VBScript 做同样的事情,但它需要额外的代码:
descr = "..."
Set wmi = GetObject("winmgmts://./root/cimv2")
Set app = CreateObject("Shell.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Function Contains(str1, str2)
Contains = InStr(LCase(str1), LCase(str2)) > 0
End Function
'Define an empty resizable array.
ReDim procs(-1)
For Each p In wmi.ExecQuery("SELECT * FROM Win32_Process")
dir = fso.GetParentFolderName(p.ExecutablePath)
exe = fso.GetFileName(p.ExecutablePath)
Set fldr = app.NameSpace(dir)
Set item = fldr.ParseName(exe)
'Determine the index of the description field.
'IIRC the position may vary, so you need to determine the index dynamically.
For i = 0 To 300
If Contains(fldr.GetDetailsOf(fldr, i), "description") Then Exit For
Next
'Check if the description field contains the string from the variable
'descr and append the PID to the array procs if it does.
If Contains(fldr.GetDetailsOf(item, i), descr) Then
ReDim Preserve procs(UBound(procs) + 1)
procs(UBound(procs)) = p.ProcessId
End If
Next
【讨论】:
wmic process where description='notepad.exe' get processed
求助
wmic /?
wmic process get /?
是这样做的方法。以上是命令,但 VBScript 可以访问相同的对象,这是使用对象 Retrieving Information from Task Manager using Powershell 的帖子。
【讨论】:
tasklist-command 可能是您的解决方案。
tasklist /FI "IMAGENAME eq MyApplication*"
它还具有以 CSV 格式输出格式的参数,这可能有助于进一步处理结果。
【讨论】: