[英]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.