16

如何使用 taskkill 按名称杀死进程并从特定路径发起?

taskkill /F /IM

当然它不能区分从两个不同位置 C:\Dir1 和 C:\Dir2 开始的 2 个进程

tasklist 是否有任何开关来获取路径名

4

5 回答 5

14

taskkill做不到。但是如果可以的话,你可以使用 PowerShell:

(Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
于 2012-11-23T06:52:02.077 回答
6

根据乔伊的回答:

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%”呢?

如果我们从 Explorer 启动这个程序,它的命令行可能会被引用。如果我们忽略它,将没有匹配项:

> 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.

因此,建议像 l0pan 的回答一样使用“ExecutablePath” 。

于 2013-11-22T17:59:58.020 回答
6

使用以下命令(即使没有 powershell 也可以使用):

wmic process where ExecutablePath='C:\\Dir1\\image.exe' delete

wmic注意:只有在 Windows 8 上以管理员身份运行时,所有进程才能访问 ExecutablePath

于 2014-05-06T18:41:05.783 回答
3

您的情况似乎是当您从不同的路径在机器上安装了具有相同进程名称的自定义服务时。如果确实是这种情况,那么您可能有不同的服务名称可以用作附加过滤器。

请参阅语法:

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 命令

于 2017-10-11T09:41:04.087 回答
2

除非您以管理员权限运行 powershell,否则您只能找到进程的路径


32位PowerShell无法通过 找到64位进程的路径Get-Process,所以我建议你使用WMI或CIM。一些可能有用的参考资料:


PowerShell 方式。基于乔伊的回答和用户的评论

假设程序的路径是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'.

另外,上述命令也避免了找不到匹配进程时的错误(例如,Pathof no process 等于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 及更高版本(CIM Cmdlets 简介 | PowerShell 团队简介Get-CimInstance)中使用而不是:Get-WmiObject

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) 上测试它。

于 2020-09-17T02:36:27.853 回答