forループ内のWMIC

forループ内のWMIC

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 expression2 行目については得られません。

最初の行は機能しているので、引用に何か問題があると思います。誰かこの点について説明してもらえませんか? 文法的に 3 回確認しましたが、この他の質問によると、すべて正しいようです。Wmic 出力を変数に

編集1: %teststr% はフィルタリングする文字列に過ぎません。たとえば、特定の Java インスタンスを検索する場合は javaw になります。

編集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 expression2番目のコマンドを取得します。

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

参考文献

関連情報