如何从用户名和imagename过滤的命令行获取PID [英] how to get PID from command line filtered by username and imagename
问题描述
我需要能够使用命令行从正在运行的进程(cmd.exe)获取PID。
问题是有两个cmd.exe运行。一个是用户名SYSTEM,一个是compUser。有没有办法我可以抓取compUser cmd.exe的PID?
I need to be able to get the PID from a running process (cmd.exe) using the command line. The problem is there are two cmd.exe running. One is under the username SYSTEM and one is compUser. Is there a way that I can grab the PID of the compUser cmd.exe?
编辑:这需要进一步的解释。
我从一个批处理文件。我在我的批处理文件中的一个调用启动一个cmd.exe从不会死。所以杀死cmd.exe很简单:
This needs further explanation. I'm doing this from a batch file. One of the calls that I'm making in my batch file starts a cmd.exe that never dies. So killing that cmd.exe would be simple:
taskkill /F /IM cmd.exe /FI "username eq compUser"
问题是我所在的批处理文件正在被另一个cmd.exe实例处理在用户名compUser下。我试图做的是在启动第二个cmd.exe之前从原始cmd.exe获取PID。这样我可以使用命令:
The problem is that the batch file that I'm in is being handled by another instance of cmd.exe under the username compUser. What I'm attempting to do is get the PID from the original cmd.exe before I start the second cmd.exe. That way I can just use the command:
taskkill /F /IM cmd.exe /FI "username eq compUser" /FI "PID neq [orignal task's PID]"
推荐答案
我最后要做的是使用:
TASKLIST /NH /FI "IMAGENAME eq cmd.exe" /FI "username eq compUser"> psid.txt
FOR /F "tokens=2" %%I in (psid.txt ) DO set pIdNotToKill=%%I
在我开始挂起的批处理脚本之前。然后当我准备杀死悬挂的cmd窗口:
right before I started the batch script that hangs. Then when I was ready to kill the hanging cmd window:
taskkill /F /IM cmd.exe /FI "PID ne %pIdNotToKill%" /FI "username eq compUser"
这可能有更好的方法,
There is probably a better way, but this worked.
这篇关于如何从用户名和imagename过滤的命令行获取PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!