2017-08-10 127 views
2

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

例如,enter image description here

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

回答

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

求助

wmic /? 
wmic process get /? 

是做到这一点的方式。上面是命令,但VBScript可以访问相同的对象,这里是使用对象Retrieving Information from Task Manager using Powershell的文章。

+0

不PowerShell中,使用CMD命令或VBScript请 – Juran

+0

这是CMD不是PowerShell中,虽然PS可以访问相同的对象,因为所有窗口语言。 – Acat

0

任务列表命令可能是一个解决方案。

tasklist /FI "IMAGENAME eq MyApplication*" 

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

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