使用VBScript获取描述的进程ID

时间:2017-08-10 05:23:16

标签: cmd vbscript

使用CMD或VBScript从给定描述中获取进程ID或映像名称的最简单方法是什么?

例如,enter image description here

Description = "My application*",我想让所有进程ID都具有该描述。

3 个答案:

答案 0 :(得分:3)

枚举进程的最佳方法是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

答案 1 :(得分:1)

wmic process where description='notepad.exe' get processed

寻求帮助

wmic /?
wmic process get /?

是这样做的。上面是命令,但VBScript可以访问相同的对象,这里是使用对象Retrieving Information from Task Manager using Powershell的帖子。

答案 2 :(得分:0)

tasklist-command可以为您提供解决方案。

{{1}}

它还具有用CSV格式化输出的参数,这可用于进一步处理结果。