[英]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.为了构建自动卸载脚本,我一直在努力使用 WMIC 批量获取特定输出。 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 (eg: C:\\ProgramData{07BFF8FA-C12F-46C7-8239-8EE83E21B5DA}\\program-name\\Uninstall.exe).
我遇到的问题是,我尝试删除的应用程序的卸载程序是在每个系统上自动生成的 SSID 下创建的(例如:C:\\ProgramData{07BFF8FA-C12F-46C7-8239-8EE83E21B5DA}\\程序名\\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.
因此,我无法根据注册表构建静态卸载位置,因为卸载程序字符串也位于注册表中的相同 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!非常感谢你们!
EDIT .Turns out that %
is needed to beused 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在这里解释
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.