【问题标题】:Need to Get Process Name From Changing PID Using Batch File需要通过使用批处理文件更改 PID 来获取进程名称
【发布时间】:2019-10-13 19:43:10
【问题描述】:

我正在随机冻结应用程序,虽然我可以使用批处理文件轻松结束未响应的任务,但我正在尝试诊断导致问题的应用程序。但是我不能引用已经被 PID 杀死的任务。我希望代码在终止进程之前或之后显示有问题的应用程序。

这是我到目前为止一直在使用的,它确实有效并且确实显示了 PID。但它不显示应用程序名称。

@echo off

:Start

taskkill /f /fi "status eq not responding"

TIMEOUT /t 30 /nobreak

goto Start


**This is what i've been trying to use to get the offending application name**

    :Start
    Set %processPID% = taskkill.exe /fi "status eq not responding"
    wmic process where "ProcessID=%processPID% get CommandLine, ExecutablePath
    taskkill.exe /f /fi "status eq not responding"
    TIMEOUT /t 5 /nobreak
    goto Start

这是我运行新代码时得到的结果

似乎没有设置变量processPID

C:\Users\razra\Desktop>Set = taskkill.exe /fi "status eq 没有​​响应" 该命令的语法不正确。

C:\Users\razra\Desktop>wmic process where "ProcessID= get CommandLine, ExecutablePath , - 无效的别名动词。

【问题讨论】:

  • 如果您尝试诊断导致问题的应用程序,您是否检查了事件查看器中的应用程序日志,它甚至有一个command line interface
  • Set %processPID% = taskkill.exe /fi "status eq not responding" 不能工作;看看这个吧:How to set commands output as a variable in a batch file
  • 事件查看器是我第一次使用。每当出现问题时,它往往会将您引导至 Windows 服务,它只是假设它是窗户破裂而不是其他东西破坏窗户。感谢您提供有关 CMD 变量的信息,它帮助我决定继续使用 powershell。

标签: batch-file cmd


【解决方案1】:

如果你愿意尝试PowerShell,你可以实现同样的功能。

  1. 循环遍历具有Responding 属性false 的进程。
  2. 按 ID 停止进程。

    Foreach($proc in Get-Process | Where-Object {$_.Responding -eq $false}){
       Write-Host $proc.ID, $proc.Name
       Stop-Process $proc.ID
    }
    
  3. 使用ps1 扩展保存代码并使用PowerShell.exe -File <path> 运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多