I am trying to process the output of wmic command in for loop:
@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
)
It prints the following:
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.
What I am trying to do is to process only:
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
It seems that the following works:
@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
But it seems that skip=2 part depends on the wmic output format.
Is it possible to get the desired string without the skip=2 part?
I tried this, but this doesn't work(not sure I understand why):
@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