WMIC in for-loop

1

1

I'm trying to get WMIC output into a variable so I can process it further.

I have made a test batch file to illustrate the problem:

wmic PROCESS where "commandline like '%%teststr%%'" get     Processid,Caption,Commandline
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

Having called this batch I get the expected output for the first line, but invalid GET expression for the second.

Since the first line does work I think there is something wrong with my quoting - could someone please shed a light on this? I triple-checked it syntactically and it all seems correct to me according to this other question: Wmic output into variable

Edit1: %teststr% is just a string to filter, it could be javaw for example to look for certain java instances.

Edit2: Exact output is:

Caption    CommandLine                                                                                                                  ProcessId
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  5152
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  11504
javaw.exe  "c:\Program Files (x86)\Java\jdk1.7.0_80\bin\javaw.exe"  -jar "j:\tools\online\JBinUp\JBinUp.jar"                            16336
WMIC.exe   wmic  PROCESS where "commandline like '%javaw%'" get Processid,Caption,Commandline                                           18740

Invalid GET Expression.

BB

beerbear

Posted 2016-05-19T16:30:28.970

Reputation: 55

What is %%teststr%%? – DavidPostill – 2016-05-19T16:49:25.440

Why do you have but ´ at the end of your first command? And Commandlin is not spelt correctly. – DavidPostill – 2016-05-19T16:50:37.103

What is the output of wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline? – DavidPostill – 2016-05-19T16:51:07.543

Please [edit] the question, fix the mistakes and add the extra information. – DavidPostill – 2016-05-19T16:51:40.387

Done - was just a paste error, in the batch file was correct. – beerbear – 2016-05-19T17:05:54.763

I still want the output from the wmic command for testing. – DavidPostill – 2016-05-19T17:10:24.103

Done. It's the output of the whole batch, lines 1-5 come from the first line of the batch, last line from the second which I try to debug, – beerbear – 2016-05-19T17:39:37.930

Never mind, I figured it out. See my answer. – DavidPostill – 2016-05-19T17:51:45.927

Answers

3

I get invalid GET expression for the second command.

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

You need to escape the , (commas) in the for expression, using the ^ Escape character:

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline`) do echo OUTPUT is %%R

Notes:

  • You might also want to add skip=1 to the for command to skip the header.
  • You will get an extra blank line at the end of the wmic output.
  • Use findstr to strip the blank lines from wmic output, as follows:
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%R

Test batch file:

@echo off
setlocal EnableDelayedExpansion
wmic process where "Commandline like '%%note%%'" get Processid,Caption,Commandline
for /f "usebackq" %%r in (`wmic process where "commandline like '%%note%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%r
endlocal

Example output:

F:\test>test
Caption                   CommandLine                                                                                                                            ProcessId
GSNotes.exe               "E:\GoldenSectionNotes\GSNotes.exe"                                                                                                    8864
LiberKeyPortabilizer.exe  "E:\LiberKey\LiberKeyTools\LiberKeyPortabilizer\LiberKeyPortabilizer.exe" /app="E:\LiberKey\Apps\Notepad++\Notepad++LKL.dat"  /lkpend  12324
notepad++.exe             "E:\LiberKey\Apps\Notepad++\App\Notepad++\notepad++.exe"                                                                               11948
WMIC.exe                  wmic  process where "Commandline like '%note%'" get Processid,Caption,Commandline                                                      1364

OUTPUT is Caption
OUTPUT is GSNotes.exe
OUTPUT is LiberKeyPortabilizer.exe
OUTPUT is notepad++.exe
OUTPUT is cmd.exe
OUTPUT is WMIC.exe

Further Reading

DavidPostill

Posted 2016-05-19T16:30:28.970

Reputation: 118 938