3

I've been struggling to get a specific output using WMIC in batch in order to build an automatic uninstall script. The issue I'm running across is that the uninstaller for the application I'm trying to remove is created under an auto-generated SSID on each system (e.g.: C:\ProgramData{07BFF8FA-C12F-46C7-8239-8EE83E21B5DA}\program-name\Uninstall.exe). Because of this, I can't build a static uninstall location based on the registry as the uninstaller string also is under the same SSID in the registry.

I've tried a couple of different ways of pulling the uninstall info and the only one I've landed on that's come close is using WMIC:

wmic product where "Name like '%product name%'" get name

which outputs:

Name
<product-name>

^ and an extra carriage return, and that carriage return is the issue. It sets the variable, then clears it.

Here's the for loop I'm trying to use to get this to work:

@echo off
for /f "skip=1 delims==" %%a in (
     'wmic product where "Name like '%product-name%' get name'
) do set PROD=%%a
echo %PROD%

which outputs:

C:\Users\Administrator>ECHO is off.

which means the %PROD% variable isn't defined at all.

If I run the batch with @echo ON, I get this:

:\Users\Administrator>echo <product-name>
<product-name>
:\Users\Administrator>echo
ECHO is on.

Notice the output is missing the drive letter. That's exactly what I'm seeing so it's weird, and also the parameter is being set, echo'd then unset.

I've also tried to do this via a text file relay:

wmic /OUTPUT:%~dp0\wmic.txt product where "Name like '%product-name%'" get name
for /f %%a in (
     "%~dp0\wmic.txt" | findstr /v "product-name"
) do set PROD=%%a

Any help/advice would be most welcome!


UPDATE!

following link provided by npocmaka, I came up with this:

for /f "skip=1 delims=" %a in ('wmic product where "Name like '%product-name%'" get name') do @for /f "delims=" %b in ("%a") do @echo %b

which correctly outputs the product name

However, when I run it from batch as:

for /f "skip=1 delims=" %%a in (
    'wmic product where "Name like '%product-name%'" get name'
) do @for /f "delims=" %%b in ("%%a") do echo %%b

I get instead:

No Instance(s) Available.

Which to me sounds like an issue WMIC is having with syntax or something


SOLVED!

Credit to npocmaka for suggesting a nested FOR loop, and indiv for pointing out the escape logic for the WMIC variable

Correct syntax of the command used in batch:

for /f "skip=1 delims=" %%a in (
     'wmic product where "Name like '%%product-name%%'" get name'
) do @for /f "delims=" %%b in ("%%a") do @echo %%b

Thanks a ton guys!

aschipfl
  • 33,626
  • 12
  • 54
  • 99
pyr0ball
  • 33
  • 1
  • 1
  • 5
  • `No Instance(s) Available.` means that no object applying the criteria was found. – npocmaka Jun 11 '15 at 22:00
  • Is the variable `%product-name%` defined ? – SachaDee Jun 11 '15 at 22:14
  • @npocmaka I know, but that's essentially the same command except formatted for batch instead of running it from a cmd prompt directly, so I'm at a loss as to why it will output from CMD but not batch – pyr0ball Jun 11 '15 at 22:39
  • @SachaDee That's a placeholder name, and in the context of the WMIC command, the "%" characters act as wildcards. When I run this command using a cmd prompt directly, it outputs the product name I want, however, when I run from batch (being sure to use %%a instead of %a) the output of the WMIC command is "No Instance(s) Available." – pyr0ball Jun 11 '15 at 22:46
  • 1
    Similar logic as `%a` -> `%%a` applies for product-name. Make it `%%product-name%%` – indiv Jun 11 '15 at 22:54
  • @indiv WOOOO! THAT DID IT~ – pyr0ball Jun 11 '15 at 23:16
  • but you didn't assign the result to the variable – Leon Barkan Feb 24 '17 at 17:55

1 Answers1

5

EDIT.Turns out that % is needed to be used as wildcard

@echo off
for /f "skip=1 delims==" %%a in (
     'wmic product where "Name like '%%product-name%%'" get name /format:table'
) do (
   for /f "tokens=* delims=" %%# in ("%%a") do  set PROD=%%a
) 
echo %PROD%

it is explained here

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 1
    the output I get from that is: `do( was unexpected at this time.` – pyr0ball Jun 11 '15 at 20:48
  • 1
    added a space after "do" and got output, but bad news. Node - ERROR: Description = Invalid query ECHO is off. – pyr0ball Jun 11 '15 at 20:54
  • Progress!, after checking that link I came up with this: `for /f "skip=1 delims=" %%a in ('wmic product where "Name like '%product-name%'" get name') do for /f "delims=" %%b in ("%%a") do echo %%b` which gave me "No Instances Available" – pyr0ball Jun 11 '15 at 21:16
  • @pyr0ball test first the query without the for loop – npocmaka Jun 11 '15 at 21:59
  • I did, and it outputs correctly. I stated as much in the original post, I'm just using a placeholder name for the product in question () – pyr0ball Jun 11 '15 at 22:47