2017-08-10 123 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