从WMIC获取批次名称的产品名称 [英] Get product name from WMIC for a variable in batch

查看:123
本文介绍了从WMIC获取批次名称的产品名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

输出:

Name
<product-name>

^和额外的回车符,那么回车符就是问题所在.它先设置变量,然后清除它.

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

这是我正在尝试使用的for循环:

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%

输出:

C:\Users\Administrator>ECHO is off.

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

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

如果在打开@echo的情况下运行批处理,则会得到以下提示:

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!

更新!

按照npocmaka提供的链接,我想到了这一点:

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

正确输出产品名称

但是,当我以如下方式批量运行时:

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

我得到了:

No Instance(s) Available.

对我来说,听起来像WMIC在语法或某些方面有问题

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

已解决!

向npocmaka表示嵌套的FOR循环,并向indiv指出WMIC变量的转义逻辑

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

谢谢你们!

推荐答案

EDIT .结果证明%必须是

在此处解释

这篇关于从WMIC获取批次名称的产品名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆