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
Best Answer
I get
invalid GET expression
for the second command.You need to escape the
,
(commas) in thefor
expression, using the^
Escape character:Notes:
skip=1
to thefor
command to skip the header.wmic
output.findstr
to strip the blank lines fromwmic
output, as follows:Test batch file:
Example output:
Further Reading