windows 从 WMIC 批量获取变量的产品名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30790561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:07:43  来源:igfitidea点击:

Get product name from WMIC for a variable in batch

windowsbatch-filecmdwmic

提问by 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.

为了构建自动卸载脚本,我一直在努力使用 WMIC 批量获取特定输出。我遇到的问题是,我尝试删除的应用程序的卸载程序是在每个系统上自动生成的 SSID 下创建的(例如:C:\ProgramData{07BFF8FA-C12F-46C7-8239-8EE83E21B5DA}\程序名\Uninstall.exe)。因此,我无法根据注册表构建静态卸载位置,因为卸载程序字符串也位于注册表中的相同 SSID 下。

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:

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:

这是我试图用来让它工作的 for 循环:

@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.

这意味着 %PROD% 变量根本没有定义。

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

如果我使用 @echo ON 运行批处理,我会得到:

:\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:

通过 npocmaka 提供的链接,我想出了这个:

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

对我来说,这听起来像是 WMIC 在语法或其他方面的问题



SOLVED!

解决了!

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

归功于 npocmaka 建议嵌套 FOR 循环,以及 indiv 指出 WMIC 变量的转义逻辑

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!

非常感谢你们!

回答by 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

它是在这里解释