【问题标题】:taskkill to differentiate 2 images by pathtaskkill 按路径区分 2 个图像
【发布时间】:2012-11-23 06:48:47
【问题描述】:

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

taskkill /F /IM

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

tasklist有没有获取路径名的开关

【问题讨论】:

    标签: windows windows-shell


    【解决方案1】:

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

    (Get-WmiObject Win32_Process | Where-Object { $_.Path.StartsWith('C:\Dir1') }).Terminate()
    

    【讨论】:

    • 对于严格相等使用$_.Path -eq "C:\Dir1\file.exe"。作为参考,hereWin32_Process 类属性的完整列表。
    【解决方案2】:

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

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

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

    【讨论】:

    • 为什么我必须使用双黑斜线? \\
    • 看起来不像是必须批量转义的字符。
    • 原因是您使用的是 wmic,而不是批处理,即使它是批处理的,您使用的是 wmic 命令,这确实需要您将其转义,因为 wmic 中的 \ 与批处理不同。
    • 天才!!谢谢
    • 对于任何对wmic 的注释感兴趣的人:windows - wmic process empty executablepath - Stack Overflow
    【解决方案3】:

    根据乔伊的回答:

    wmic Path win32_process Where "CommandLine Like '%C:\\Dir1\\image.exe%'" Call Terminate
    

    当 Path 为 null(不知道为什么)并且不需要 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的answer这样的“ExecutablePath”。

    【讨论】:

      【解决方案4】:

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

      见语法:

      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 command

      【讨论】:

        【解决方案5】:

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


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


        PowerShell 方式。基于 Joey 的answer 和用户的comment

        假设程序的路径是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 及更高版本 (Introduction to CIM Cmdlets | PowerShell Team) 中使用 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) 上测试。

        【讨论】:

          猜你喜欢
          • 2022-12-18
          • 1970-01-01
          • 2015-01-18
          • 1970-01-01
          • 2017-12-16
          • 1970-01-01
          • 1970-01-01
          • 2016-10-01
          • 1970-01-01
          相关资源
          最近更新 更多