Search code examples
batch-filewmic

WMIC Capturing Output


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


Solution

  • 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.