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 expression두 번째 줄에서는 예상되는 출력을 얻습니다.

첫 번째 줄이 작동하므로 내 인용에 문제가 있는 것 같습니다. 누군가 이에 대해 설명해 주시겠습니까? 나는 그것을 구문론적으로 세 번 확인했고 다음 질문에 따르면 모든 것이 올바른 것 같습니다.Wmic 출력을 변수로

편집 1: %teststr%는 필터링할 문자열일 뿐이며, 예를 들어 특정 Java 인스턴스를 찾는 데에는 javaw가 될 수 있습니다.

Edit2: 정확한 출력은 다음과 같습니다.

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

Escape 문자를 사용하여 표현식 ,에서 (쉼표)를 이스케이프해야 합니다 .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

추가 자료

관련 정보