[英]taskkill to differentiate 2 images by path
如何使用taskkill按名稱殺死進程並從特定路徑發起?
任務殺死 /F /IM
當然它無法區分從兩個不同位置啟動的 2 個進程 C:\\Dir1 和 C:\\Dir2
任務列表是否有任何開關來獲取路徑名
taskkill
做不到。 但是,如果可以選擇,您可以使用 PowerShell:
(Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
基於喬伊的回答:
wmic Path win32_process Where "CommandLine Like '%C:\\Dir1\\image.exe%'" Call Terminate
這樣,當 Path 為空(不知道為什么)並且不需要 PowerShell 時,我可以避免 NullReferenceException。
參考: https : //superuser.com/questions/52159/kill-a-process-with-a-specific-command-line-from-command-line
警告
如果有其他使用命令行運行的進程包含該圖像路徑,則很危險。 例如:
> start cmd /k "C:\windows\system32\notepad.exe"
> wmic Path win32_process where "CommandLine Like '%C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
Caption CommandLine ExecutablePath ProcessId
cmd.exe cmd /k "C:\windows\system32\notepad.exe" C:\WINDOWS\system32\cmd.exe 11384
notepad.exe C:\windows\system32\notepad.exe C:\windows\system32\notepad.exe 9684
那么...如果我們使用“C:\\Dir1\\image.exe%”而不是“%C:\\Dir1\\image.exe%”會怎樣?
如果我們從資源管理器啟動這個程序,它的命令行可能會被引用。 如果我們忽略它,將沒有匹配項:
> wmic Path win32_process where "CommandLine Like '%C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
Caption CommandLine ExecutablePath ProcessId
notepad.exe "C:\WINDOWS\system32\notepad.exe" C:\WINDOWS\system32\notepad.exe 108
> wmic Path win32_process where "CommandLine Like 'C:\\Windows\\system32\\notepad.exe%'" get caption,processid,executablePath,commandline
No Instance(s) Available.
因此,建議使用“ExecutablePath”,如 l0pan 的回答。
使用以下命令(即使沒有 powershell 也能工作):
wmic process where ExecutablePath='C:\\Dir1\\image.exe' delete
注意:僅當您在 Windows 8 上以管理員身份運行wmic
,所有進程都可以訪問 ExecutablePath
您的情況似乎是當您從不同路徑在機器上安裝了具有相同進程名稱的自定義服務時。 如果確實是這種情況,您可能有不同的服務名稱,可用作附加過濾器。
參見語法:
taskkill /S {REPLACE_WITH_SERVER_IP_OR_NAME} /F /FI "IMAGENAME eq {REPLACE_WITH_PROCESS_NAME}" /FI "SERVICES eq {REPLACE_WITH_SERVICENAME}"
參見示例:
taskkill /S 10.10.1.1 /F /FI "IMAGENAME eq tomcat7.exe" /FI "SERVICES eq tomcatServiceEngine"
有關所有可用過濾器的列表,請訪問taskkill 命令
您只能除非你運行的是具有管理員權限的PowerShell找到你的進程的路徑。
32 位 PowerShell 無法通過Get-Process
找到 64 位進程的路徑,因此建議您使用 WMI 或 CIM。 一些可能有用的參考資料:
假設程序的路徑是C:\\Dir1\\file.exe
。 如果程序的多個實例正在運行,您應該使用以下命令:
Get-WmiObject Win32_Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
ForEach-Object { $_.Terminate() }
否則Powershell會報錯:
PS > (Get-WmiObject Win32_Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" }).Terminate()
Method invocation failed because [System.Object[]] doesn't contain a method named 'Terminate'.
另外,上面的命令還避免了找不到匹配進程時的錯誤(例如,沒有進程的Path
等於C:\\Dir1\\file.exe
):
PS > (Get-WmiObject Win32_Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" }).Terminate()
You cannot call a method on a null-valued expression.
如果您不喜歡 WMI:
Get-Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
ForEach-Object { Stop-Process -Id $_.Id }
我注意到Win32_Process.Terminate()
和Stop-Process
都是用來強制終止進程的,所以進程可能無法執行任何清理工作。
在 Windows 7 (6.1.7600.0) 上的 Powershell 2.0 中測試。
如果您確實喜歡 WMI 並且您使用的是 Windows 6.2(Windows Server 2012 和 Windows 8)及更高版本,則應在 PowerShell 3 及更高版本中使用Get-CimInstance
而不是Get-WmiObject
( CIM Get-CimInstance
簡介 | PowerShell 團隊):
Get-CimInstance Win32_Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
Invoke-CimMethod -MethodName "Terminate"
或者,更多的 CIM'y:
Get-CimInstance CIM_Process |
Where-Object { $_.Path -eq "C:\Dir1\file.exe" } |
Invoke-CimMethod -MethodName "Terminate"
在這里,您可能想知道為什么Invoke-CimMethod
:
而且,為什么“CIM”:
我沒有在 Windows 6.2 上測試過,而是在 Windows 10 (10.0.19041.0) 上測試過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.