Batch - FOR Loop to Turn WMIC Output Into Variable Not Working

850 views Asked by At

I've been trying to turn the output of the following two commands into variables, so that I can use them in a batch file, however I'm not having any luck:

WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE (displayName="Emsisoft Anti-Malware" or displayName="Emsisoft Internet Security") GET displayName /value
WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE (displayName="Emsisoft Anti-Malware" or displayName="Emsisoft Internet Security") GET pathToSignedReportingExe /value

Basically two different security products can potentially be installed, and the batch file needs to determine which one. Since the folder can potentially be changed during install, the safest way to determine that seems to be with the above two commands. Sadly, I can't get the output into variables in order to use it.

Here's my current test script:

@ECHO OFF

for /f "tokens=2 delims==" %%f in ('WMIC /namespace:\\\\root\\SecurityCenter2 PATH AntiVirusProduct WHERE ^(displayName^="Emsisoft Anti-Malware" or displayName^="Emsisoft Internet Security"^) GET DisplayName /value ^| find "="') do set "EmsiProductName=%%f"
ECHO %EmsiProductName%

for /f "tokens=2 delims==" %%f in ('WMIC /namespace:\\\\root\\SecurityCenter2 PATH AntiVirusProduct WHERE ^(displayName^="Emsisoft Anti-Malware" or displayName^="Emsisoft Internet Security"^) GET pathToSignedReportingExe /value ^| find "="') do set "EmsiProductPath=%%f"
ECHO %EmsiProductPath%

PAUSE

When run, the above outputs two errors that say "Description = The RPC server is unavailable." I've tried escaping the quotes (both "" and \"), and not escaping them, but the output is the same either way.

I'm out of ideas, so if anyone has any suggestions, then I would greatly appreciate it.

2

There are 2 answers

4
GT500 On BEST ANSWER

Since most of the answers have been posted in comments, I'll explain what resolved this for those who are curious (and for those who are still posting comments under the assumption that this has not been resolved).

As both Noodles and aschipfl have pointed out, escaping the backslashes was not necessary, and was causing problems. Unfortunately that was not the only problem with my code. As rojo mentioned, there seems to have been an issue with escaping other characters as well, and the parenthesis seemed to be messing things up. The best solution was to remove all of the escaping, and use quotes instead of parenthesis. rojo also recommended I use like instead of = to match the values I was looking for, which worked beautifully .

For those who would like to see the working code, this is what I ended up with:

FOR /F "tokens=2 delims==" %%F IN ('WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE "displayName like 'Emsisoft%%'" GET DisplayName /value ^| FIND "="') DO SET "EmsiProduct=%%F"
FOR /F "tokens=2 delims==" %%F IN ('WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE "displayName like 'Emsisoft%%'" GET pathToSignedReportingExe /value ^| FIND "="') DO SET "TempEmsiProductDir=%%F"
SET EmsiProductDir=%TempEmsiProductDir:~0,-15%
SET EmsiProductDirDoubleSlash=%EmsiProductDir:\=\\%

For those who are curious about the third line, I only needed the path to the folder, so I am omitting the last 15 characters to remove the last backslash and file name.

As for the fourth line, part of the batch file calls WMIC to get file properties, and for that you need paths with double backslashes (or it won't work), so rather than use another FOR loop to try to change that I am using :\=\\ on the end of the variable name to convert the backslashes into double backslashes. I think this is where I got the idea from, however it looks like there is a more in-depth explanation of it in this article.

0
Compo On

Here is an attempt at how I may go about this task using the like comparison as has previously been mentioned.

@Echo Off
For /F "UseBackQ Skip=1 Delims=" %%A In (`WMIC^
 /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct^
 Where "DisplayName Like 'Emsisoft%%'" Get pathToSignedProductExe`) Do (
    For /F "Tokens=*" %%B In ("%%~dpA") Do Set "EmsiProductPath=%%~B"
Set EmsiProductPath
Timeout -1

It is completely untested, because even if I was using a Windows PC I wouldn't have an antivirus product installed.