Home > Enterprise >  How to filter out wmic process when searching for a process with a specific command line using wmic?
How to filter out wmic process when searching for a process with a specific command line using wmic?

Time:03-23

I tried to find an application by the name myapp directly from command prompt and from a batch script by running the following command:

D:\Files>wmic process where "commandline like '%myapp%'" get processid, commandline

CommandLine                                                                  ProcessId  
wmic  process where "commandline like '%myapp%'" get processid, commandline  10744     
myapp myarg1 myarg2 myarg3                                                   2423 

I want to filter out the wmic process entry itself. I tried the following command:

D:\Files>wmic process where "commandline like '%myapp%' and commandline not like '%wmic%'" get processid, commandline

Node - XXXXXXXXXXX
ERROR:
Description = Invalid query

But it outputs an error as shown above.

I tried manually skipping the first line with more 1 but it may happen that the order of output lines (processes) varies.

What could be done to remove the wmic process entry?

CodePudding user response:

Specify any one character within a range by surrounding it with square brackets [].

Using the java example in your comments:

In a enter image description here

example (Linux' grep):

ps -ef | grep "[t]nslsnr"

Result with and without []

enter image description here

CodePudding user response:

The correct way to run your particular command and return only the instance(s) you require, directly in , is this:

wmic process where "commandline like '%myapp%' and not commandline like 'wmic%'" get processid, commandline

The idea of this mechanism is to exclude the wmic command line itself. It does that by asking for command lines which include anywhere the string myapp but do not include command lines which begin with the string wmic.

If this command is run within a , the percent characters would require doubling, i.e.

wmic process where "commandline like '%%myapp%%' and not commandline like 'wmic%%'" get processid, commandline
  • Related