WMIC 在 for 循环中

WMIC 在 for 循环中

我正在尝试将 WMIC 输出放入变量中,以便进一步处理它。

我制作了一个测试批处理文件来说明这个问题:

wmic PROCESS where "commandline like '%%teststr%%'" get     Processid,Caption,Commandline
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

调用此批处理后,我得到了第一行的预期输出,但invalid GET expression得到了第二行的预期输出。

由于第一行确实有效,我认为我的引用有问题 - 有人可以解释一下吗?我从语法上对其进行了三次检查,根据另一个问题,在我看来一切都是正确的:Wmic 输出到变量

编辑1:%teststr%只是一个要过滤的字符串,它可以是javaw,例如查找某些java实例。

编辑2:确切的输出是:

Caption    CommandLine                                                                                                                  ProcessId
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  5152
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  11504
javaw.exe  "c:\Program Files (x86)\Java\jdk1.7.0_80\bin\javaw.exe"  -jar "j:\tools\online\JBinUp\JBinUp.jar"                            16336
WMIC.exe   wmic  PROCESS where "commandline like '%javaw%'" get Processid,Caption,Commandline                                           18740

Invalid GET Expression.

BB

答案1

我得到了invalid GET expression第二条命令。

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

您需要使用转义字符来转义表达式,中的(逗号) :for^

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline`) do echo OUTPUT is %%R

笔记:

  • 您可能还想添加skip=1命令 for以跳过标题。
  • 您将在输出末尾看到一个额外的空白行wmic
  • 用于findstr从输出中删除空白行wmic,如下所示:
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%R

测试批处理文件:

@echo off
setlocal EnableDelayedExpansion
wmic process where "Commandline like '%%note%%'" get Processid,Caption,Commandline
for /f "usebackq" %%r in (`wmic process where "commandline like '%%note%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%r
endlocal

示例输出:

F:\test>test
Caption                   CommandLine                                                                                                                            ProcessId
GSNotes.exe               "E:\GoldenSectionNotes\GSNotes.exe"                                                                                                    8864
LiberKeyPortabilizer.exe  "E:\LiberKey\LiberKeyTools\LiberKeyPortabilizer\LiberKeyPortabilizer.exe" /app="E:\LiberKey\Apps\Notepad++\Notepad++LKL.dat"  /lkpend  12324
notepad++.exe             "E:\LiberKey\Apps\Notepad++\App\Notepad++\notepad++.exe"                                                                               11948
WMIC.exe                  wmic  process where "Commandline like '%note%'" get Processid,Caption,Commandline                                                      1364

OUTPUT is Caption
OUTPUT is GSNotes.exe
OUTPUT is LiberKeyPortabilizer.exe
OUTPUT is notepad++.exe
OUTPUT is cmd.exe
OUTPUT is WMIC.exe

进一步阅读

相关内容