WMIC Capturing Output

81 Views Asked by At

I'm trying to create batch file that will use wmic to find a process and terminate it. The command line looks like this:

"C:\Progress\OE117\bin\_progres"  -ininame Pro2.ini -basekey "INI" -b -pf C:\Progress\Pro2-XXXTest\bprepl\Scripts\replProc.pf -p bprepl\RunReplProc.p -param "Thread=33"

and using wmic I have managed to find the process thus:

wmic process where "name like '_progres.exe'" get processid,commandline

which results in this:

"C:\Progress\OE117\bin\_progres"  -ininame Pro2.ini -basekey "INI" -b -pf C:\Progress\Pro2-XXXTest\bprepl\Scripts\replProc.pf -p bprepl\RunReplProc.p -param "Thread=33"

                   11780

Now here is the problem. There are scores of processes looking very much like each other. I want to filter on a substring of the -pf parameter on the command line - in this case "Pro2-XXXTest" and the "Thread=nn" at the end of the command line Essentially what I want to do is:

Find a process where the the executable is _progres.exe and the pf parameter matches a given string (in this case "Pro2-XXXTest") and that has "Thread=nn" (the nn will come from the batch file) and terminate it.

I've managed to get it as far as the output shown above but cannot work out how I can filter in the process name and the parameter name at the same time.

Would appreciate any tips.

Thanks

Nigel

2

There are 2 best solutions below

1
Compo On BEST ANSWER

You simply need to construct a LIKE operator pattern.

Here's a very simple one:

@%SystemRoot%\System32\wbem\WMIC.exe Process Where "Name='_progres.exe' And CommandLine Like '%%\\Pro2-XXXTest\\%%'" Call Terminate

This should find all _progres.exe processes where the CommandLine property contains, anywhere within it, the string \Pro2-XXXTest\, and try to Terminate any found.

Here's a slightly less simple one:

@%SystemRoot%\System32\wbem\WMIC.exe Process Where "Name='_progres.exe' And CommandLine Like '%%\\Pro2-XXXTest\\%% %%Thread=33%%'" Call Terminate

This should find all _progres.exe processes where the CommandLine property contains, anywhere within it, the string \Pro2-XXXTest\ followed by at least one space character and the string Thread=33, and try to Terminate any found.

0
Magoo On
@ECHO OFF
SETLOCAL

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q76759067.txt"

SET "target=%~1"
SET "thread="

FOR /f "delims=" %%e IN ('type "%filename1%"') DO (
 rem Boolean flag
 SET "foundproc="
 rem analyse each element
 FOR %%y IN (%%e) DO (
  IF DEFINED foundproc (
   FOR /f "tokens=1*delims==" %%b IN ("%%~y") DO (
    IF /i "%%b"=="thread" SET "Thread=%%c"
   )
  )
  ECHO %%~y|FIND "\%target%\">NUL
  IF NOT ERRORLEVEL 1 SET "foundproc=y"
 )
)

SET thread

GOTO :EOF

This is a test which would be invoked by thisbatch Pro2-XXXTest

I've used a file for input since I can't invoke your wmic command to produce your example result. You should just need to replace the type filename command with your wmic command.