Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

windows - Get product name from WMIC for a variable in batch

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-nameUninstall.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:UsersAdministrator>ECHO is off.

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

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

:UsersAdministrator>echo <product-name>
<product-name>
:UsersAdministrator>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:%~dp0wmic.txt product where "Name like '%product-name%'" get name
for /f %%a in (
     "%~dp0wmic.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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...