how to get PID from command line filtered by username and imagename

后端 未结 4 1384
广开言路
广开言路 2020-12-06 14:45

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

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 15:01

    The way I ended up having to do this was use:

    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
    

    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.

提交回复
热议问题