Get product name from WMIC for a variable in batch

pyr0ball

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!

npocmaka

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get value of nested variable name in batch script

From Dev

Get value of nested variable name in batch script

From Dev

Using wmic to get workgroup in a batch file

From Dev

Prestashop get the product default image from the product name?

From Dev

Prestashop get the product default image from the product name?

From Dev

Get a variable name from database

From Dev

Get the result of a WMIC command and store it in a variable

From Dev

Get a substring from a Windows batch variable

From Dev

Is it possible to get a variable from a string in batch?

From Dev

How can I set a WMIC Variable in a batch file?

From Dev

Colon in batch variable name

From Dev

Get the product name in Woocommerce

From Dev

How to get the Percentage of memory utilization using WMIC in batch script?

From Dev

Windows Batch: Set variables for FOR loop, WMIC diskdrive get size

From Dev

Remove unwanted path name from %path% variable via batch

From Dev

How I can write the name of a file from a variable in Batch

From Dev

Magento - get specific category name from product id

From Java

Get label name from product WooCommerce shipping class Id

From Dev

How to get product manufacturer name from manufacturer value id by mysql?

From Dev

How to get the category name from an id product in woocommerce

From Dev

How to get product manufacturer name from manufacturer value id by mysql?

From Dev

Batch for loop using wmic

From Dev

Get name of function from loop variable

From Dev

Symfony call get by Name from variable

From Dev

How to hide get variable name from url

From Dev

Get variable name from NameError object

From Dev

Get The Class Name From an Object Variable

From Dev

How to get the array name from a variable

From Dev

Java - get variable name from double array

Related Related

  1. 1

    Get value of nested variable name in batch script

  2. 2

    Get value of nested variable name in batch script

  3. 3

    Using wmic to get workgroup in a batch file

  4. 4

    Prestashop get the product default image from the product name?

  5. 5

    Prestashop get the product default image from the product name?

  6. 6

    Get a variable name from database

  7. 7

    Get the result of a WMIC command and store it in a variable

  8. 8

    Get a substring from a Windows batch variable

  9. 9

    Is it possible to get a variable from a string in batch?

  10. 10

    How can I set a WMIC Variable in a batch file?

  11. 11

    Colon in batch variable name

  12. 12

    Get the product name in Woocommerce

  13. 13

    How to get the Percentage of memory utilization using WMIC in batch script?

  14. 14

    Windows Batch: Set variables for FOR loop, WMIC diskdrive get size

  15. 15

    Remove unwanted path name from %path% variable via batch

  16. 16

    How I can write the name of a file from a variable in Batch

  17. 17

    Magento - get specific category name from product id

  18. 18

    Get label name from product WooCommerce shipping class Id

  19. 19

    How to get product manufacturer name from manufacturer value id by mysql?

  20. 20

    How to get the category name from an id product in woocommerce

  21. 21

    How to get product manufacturer name from manufacturer value id by mysql?

  22. 22

    Batch for loop using wmic

  23. 23

    Get name of function from loop variable

  24. 24

    Symfony call get by Name from variable

  25. 25

    How to hide get variable name from url

  26. 26

    Get variable name from NameError object

  27. 27

    Get The Class Name From an Object Variable

  28. 28

    How to get the array name from a variable

  29. 29

    Java - get variable name from double array

HotTag

Archive