1

I need to get pid from below command which works if I use it in command line like below:

wmic process where "name="java.exe" and CommandLine like '%%cassandra%%'" get ProcessId | FINDSTR /v ProcessId | FINDSTR /r /v "^$"

but fails if used in .bat file:

FOR /F "tokens=2" %%G IN ('wmic process where ^(name^="java.exe"^ and CommandLine like '%%cassandra%%') get ProcessId ^| FINDSTR /v ProcessId ^| FINDSTR /r /v "^$"') DO (
   set PARENT_PID=%%G
)
echo !PARENT_PID!

Can anyone help in this?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Stack_IQ
  • 438
  • 5
  • 20
  • 1
    Fails? Fails how? What error message do you get? What does the the raw output of the `wmic` line show for you? For me, 'processname' and the PID are on separate lines. Are you aware that `WMIC` output is Unicode? – Magoo Nov 05 '20 at 07:45
  • It should also be noted that WMIC is deprecated. See this post: https://stackoverflow.com/questions/57121875/what-can-i-do-about-wmic-is-deprecated – Tim Nov 13 '20 at 20:45
  • This is the PowerShell equivalent: `get-ciminstance win32_process -Filter "name = 'notepad.exe' and commandline like '%system32%'" | %{$_.ProcessId}` – Tim Nov 13 '20 at 21:04

2 Answers2

2

Your escape points are incorrect and not sure if you enabled delayedexpansion, but I will not be using it in this example:

@echo off
for /F "tokens=2 delims==" %%G IN ('wmic process where (name^="java.exe" and commandline like '%%cassandra%%'^) get ProcessId /value') DO if not defined parent_pid set "parent_pid=%%G"
echo %parent_pid%

You will note that I used the /value switch for wmic and removed the findstr /v command, because I am using = as delimiter for splitting the value.

Also note, if you have more than one process running with the same commandline and name, you will only get one result, because you only set a value once, so in that case, do not set a variable. i.e:

@echo off
for /F "tokens=2 delims==" %%G IN ('wmic process where (name^="java.exe" and commandline like '%%cassandra%%'^) get ProcessId /value') DO echo %%G
Gerhard
  • 22,678
  • 7
  • 27
  • 43
1

The code you use within the for /F loop is not the same as the one you tried in Command Prompt as there are additional parentheses. Then you are escaping the opening ( but forgot the closing ), which is more important as it ends the parenthesised block behind the in keyword.

Anyway, you should change the wmic command line and quote the whole where clause:

wmic Process where "Name='java.exe' and CommandLine like '%%cassandra%%'" get ProcessId

So you do not have to bother to build escape sequences when using this inside of for /F:

set "PARENT_PID="
for /F "skip=1 delims=" %%P in ('
    wmic Process where "Name='java.exe' and CommandLine like '%%cassandra%%'" get ProcessId
') do for /F "tokens=1" %%Q in ("%%P") do set "PARENT_PID=%%Q"
echo/%PARENT_PID%

The second for /F loop is used to remove arefacts (orphaned carriage-return characters) coming from the Unicode-to-ASCII/ANSI conversion of the wmic command by for /F.

aschipfl
  • 33,626
  • 12
  • 54
  • 99