批处理-FOR循环将WMIC输出转换为变量不起作用 [英] Batch - FOR Loop to Turn WMIC Output Into Variable Not Working

查看:86
本文介绍了批处理-FOR循环将WMIC输出转换为变量不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图将以下两个命令的输出转换为变量,以便可以在批处理文件中使用它们,但是我没有运气:

I've been trying to turn the output of the following two commands into variables, so that I can use them in a batch file, however I'm not having any luck:

WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE (displayName="Emsisoft Anti-Malware" or displayName="Emsisoft Internet Security") GET displayName /value
WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE (displayName="Emsisoft Anti-Malware" or displayName="Emsisoft Internet Security") GET pathToSignedReportingExe /value

基本上可以安装两个不同的安全产品,并且批处理文件需要确定哪个.由于在安装过程中可能会更改文件夹,因此,最安全的方法似乎是使用上述两个命令来确定该文件夹.可悲的是,我无法将输出转换成变量才能使用它.

Basically two different security products can potentially be installed, and the batch file needs to determine which one. Since the folder can potentially be changed during install, the safest way to determine that seems to be with the above two commands. Sadly, I can't get the output into variables in order to use it.

这是我当前的测试脚本:

Here's my current test script:

@ECHO OFF

for /f "tokens=2 delims==" %%f in ('WMIC /namespace:\\\\root\\SecurityCenter2 PATH AntiVirusProduct WHERE ^(displayName^="Emsisoft Anti-Malware" or displayName^="Emsisoft Internet Security"^) GET DisplayName /value ^| find "="') do set "EmsiProductName=%%f"
ECHO %EmsiProductName%

for /f "tokens=2 delims==" %%f in ('WMIC /namespace:\\\\root\\SecurityCenter2 PATH AntiVirusProduct WHERE ^(displayName^="Emsisoft Anti-Malware" or displayName^="Emsisoft Internet Security"^) GET pathToSignedReportingExe /value ^| find "="') do set "EmsiProductPath=%%f"
ECHO %EmsiProductPath%

PAUSE

运行时,上面的内容输出两个错误,内容为描述= RPC服务器不可用".我已经尝试了对引号(" \")进行转义,并且不对它们进行转义,但是两种方法的输出都是相同的.

When run, the above outputs two errors that say "Description = The RPC server is unavailable." I've tried escaping the quotes (both "" and \"), and not escaping them, but the output is the same either way.

我没有主意,所以如果有人有任何建议,那么我将不胜感激.

I'm out of ideas, so if anyone has any suggestions, then I would greatly appreciate it.

推荐答案

由于大多数答案已发布在评论中,因此我将为好奇的人(以及仍然在下面的评论中发表评论的人)说明解决此问题的方法假设尚未解决).

Since most of the answers have been posted in comments, I'll explain what resolved this for those who are curious (and for those who are still posting comments under the assumption that this has not been resolved).

面条 aschipfl 已经指出,转义反斜杠是没有必要的,并且会引起问题.不幸的是,这不是我的代码的唯一问题.正如 rojo 所述,转义其他字符似乎也存在问题,并且括号似乎使事情变得混乱了.最好的解决方案是删除所有转义符,并使用引号而不是括号.rojo还建议我使用 like 而不是 = 来匹配我一直在寻找的值,效果很好.

As both Noodles and aschipfl have pointed out, escaping the backslashes was not necessary, and was causing problems. Unfortunately that was not the only problem with my code. As rojo mentioned, there seems to have been an issue with escaping other characters as well, and the parenthesis seemed to be messing things up. The best solution was to remove all of the escaping, and use quotes instead of parenthesis. rojo also recommended I use like instead of = to match the values I was looking for, which worked beautifully .

对于那些想看工作代码的人来说,这就是我最终得到的结果:

For those who would like to see the working code, this is what I ended up with:

FOR /F "tokens=2 delims==" %%F IN ('WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE "displayName like 'Emsisoft%%'" GET DisplayName /value ^| FIND "="') DO SET "EmsiProduct=%%F"
FOR /F "tokens=2 delims==" %%F IN ('WMIC /namespace:\\root\SecurityCenter2 PATH AntiVirusProduct WHERE "displayName like 'Emsisoft%%'" GET pathToSignedReportingExe /value ^| FIND "="') DO SET "TempEmsiProductDir=%%F"
SET EmsiProductDir=%TempEmsiProductDir:~0,-15%
SET EmsiProductDirDoubleSlash=%EmsiProductDir:\=\\%

对于那些对第三行感到好奇的人,我只需要该文件夹的路径,所以我省略了最后15个字符以删除最后一个反斜杠和文件名.

For those who are curious about the third line, I only needed the path to the folder, so I am omitting the last 15 characters to remove the last backslash and file name.

对于第四行,批处理文件的一部分调用WMIC来获取文件属性,为此,您需要使用双反斜杠的路径(否则它将不起作用),因此不要使用另一个FOR循环来尝试更改我在变量名末尾使用:\ = \\ 将反斜杠转换为双反斜杠.我认为是我从中获得灵感的地方,但是似乎对此有更深入的解释在本文中.

As for the fourth line, part of the batch file calls WMIC to get file properties, and for that you need paths with double backslashes (or it won't work), so rather than use another FOR loop to try to change that I am using :\=\\ on the end of the variable name to convert the backslashes into double backslashes. I think this is where I got the idea from, however it looks like there is a more in-depth explanation of it in this article.

这篇关于批处理-FOR循环将WMIC输出转换为变量不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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