Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tasklist to filter by multiple PIDs

I'm using tasklist command.

I'm trying to use the /fi option to filter multiple PIDs.

Attempt

tasklist.exe /v /fi "PID eq 3248" /fi "PID eq 9488"

Result

INFO: No tasks are running which match the specified criteria.

This doesn't work. I can only assume the filters are evaluated internally using logical-and and obviously would never be true.

Question

How to filter by multiple PIDs?

Ugly alternative 1

If I run it separately, the result are OK and I can set the process information. However,

  • tasklist.exe /v /fi "PID eq 3248"
  • tasklist.exe /v /fi "PID eq 9488"

I'd like to refrain activating two separate commands.

Ugly alternative 2

use find

tasklist.exe /v  | find /i "9488"

Which brings the questions:

  1. How to I find multiple PIDs?
  2. How to make sure the found strings are really the PID and NOT anything else.
like image 332
idanshmu Avatar asked Dec 14 '16 08:12

idanshmu


1 Answers

tasklist is not able to filter to several PIDs. So use full output and use another method to filter:

use csv as output format; PID is token2, windowtitle is token9.
findstr is able to search for more than one string (separated by spaces here).
/x checks "complete line", so 45 would not match 3456.
>nul supresses output of findstr (we need only the errorlevel, not the actual output)
&& acts as "if previous command (findstr) was successful, then..."

@echo off
for /f "tokens=2,9 delims=," %%a in ('tasklist /v /fo csv') do (
  echo %%~a|findstr /x "3248 9488" >nul && echo %%~a    %%~b
)
like image 142
Stephan Avatar answered Oct 05 '22 03:10

Stephan