我正在嘗試在 for 回圈中處理 wmic 命令的輸出:
@echo off
FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
echo %%a
)
它列印以下內容:
ECHO is off.
ECHO is off.
CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0
ECHO is off.
ECHO is off.
CommandLine=C:\Windows\system32\cmd.exe /c wmic process where "commandline like '%w3wp.exe%SharePoint%-h%config%'" get commandline /VALUE /format:value
ECHO is off.
ECHO is off.
CommandLine=wmic process where "commandline like '%w3wp.exe%SharePoint%-h%config%'" get commandline /VALUE /format:value
ECHO is off.
ECHO is off.
ECHO is off.
我想要做的只是處理:
CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0
似乎以下作業:
@echo off
FOR /F "tokens=* skip=2" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
SET OUTPUT=%%a & goto :next
)
:next
echo %OUTPUT%
#rem CommandLine=c:\windows\system32\inetsrv\w3wp.exe -ap "SharePoint Central Administration v4" -v "v4.0" -l "webengine4.dll" -a \\.\pipe\iisipm8d1b7d9b-4aee-49b5-87f0-1f5e78228544 -h "C:\inetpub\temp\apppools\SharePoint Central Administration v4\SharePoint Central Administration v4.config" -w "" -m 0
但似乎 skip=2 部分取決于 wmic 輸出格式。
是否可以在沒有 skip=2 部分的情況下獲得所需的字串?
我試過這個,但這不起作用(不確定我明白為什么):
@echo off
setlocal EnableDelayedExpansion
set "substring=SharePoint"
FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value') do (
set "string=%%a"
if "!string:%substring%=!"=="!string!" (
echo(!string!
goto :next
)
)
:next
endlocal
uj5u.com熱心網友回復:
在上面的特定示例中,您需要確保僅隔離您所關注的行程,而不是同時捕獲您在批處理檔案中使用的命令列。
您可以通過包含NOT LIKE過濾器來做到這一點:
@Echo Off
SetLocal EnableExtensions
For /F "Tokens=1,* Delims==" %%G In ('
%SystemRoot%\System32\wbem\WMIC.exe Process Where
"CommandLine Like '%%w3wp.exe%%SharePoint%%-h%%config%%' And Not CommandLine Like '%%WMIC.exe%%'"
Get CommandLine /Format:List 2^>NUL') Do For /F "Tokens=*" %%I In ("%%H"
) Do Echo %%I
Pause
或者,(和 IMO 更好地基于您提供的輸出),首先按已知NAME的可執行檔案過濾:
@Echo Off
SetLocal EnableExtensions
For /F "Tokens=1,* Delims==" %%G In ('
%SystemRoot%\System32\wbem\WMIC.exe Process Where
"Name = 'w3wp.exe' And CommandLine Like '%%w3wp.exe%%SharePoint%%-h%%config%%'"
Get CommandLine /Format:List 2^>NUL') Do For /F "Tokens=*" %%I In ("%%H"
) Do Echo %%I
Pause
uj5u.com熱心網友回復:
WMIC 正在輸出額外的 CRLF 集。這就是您看到 ECHO 關閉錯誤的原因。因此,只需將 WMIC 命令通過管道傳遞給 findstr 即可獲得您想要的輸出。
FOR /F "tokens=*" %%a in ('wmic process where "commandline like '%%w3wp.exe%%SharePoint%%-h%%config%%'" get commandline /VALUE /format:value ^|findstr /B /I "CommandLine=') do (
echo %%a
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312624.html
上一篇:一行中的for回圈輸出
