How to get process ID of a specific process using wmic and check on error?

12,260

Solution 1

Thanks for all the help guys. I solved my problem by using TASKLIST instead of WMIC.

WMIC gave me the output, starting with 2 non-ascii characters, and 2 lines of not usable information. I had to filter these, what worked.

@echo off
for /f "skip=1 tokens=*" %%i in ('wmic process where name^="chrome.exe" get Processid') do echo %%i

This kinda worked, but when the program I tried to 'watch' wasn't running it gave me a random pid, I think the pid I got was from the WIDC process that was looking up the requested process id.

Thats why I tried to use TASKLIST and search the full output with FINDSTR to find the correct pid I was looking for. This worked for me.

for /f "tokens=2 delims=," %%P in ('tasklist /v /fo csv ^| findstr /i "WatchdogStarterBATCH"') do set pid=%%~P

Thanks for all the help!:)

Solution 2

You could use following batch code to get either the process identifier value or the error message assigned to variable ProcessID.

@echo off
set "ProcessID="
for /F "usebackq skip=1 delims=" %%I in (`wmic process where "Name like '%%cmd.exe%%' and CommandLine like '%%WatchdogStarter.bat%%'" get processid 2^>nul`) do set "ProcessID=%%I"
if "%ProcessID%" == "" (
    echo PID could not be determined.
) else (
    echo PID is: %ProcessID%
)

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • for /?
  • if /?
  • set /?
  • wmic /?

See also Using command redirection operators to understand 2>nul which redirects error message No Instance(s) Available. (English Windows) written to STDERR to device NUL. The angle bracket in 2>nul must be escaped with ^ to apply 2>nul to command wmic instead of command FOR.

Redirecting error message to device NUL and making sure ProcessID does not exist if wmic command was not successful is better than comparing output of wmic with a specific string as the error message depends on language of Windows. This solution is independent on language of Windows.

Solution 3

I also think you are having issues with the quotes (or double quotes as in my example): e.g. I have following piece of code in my scripts:

IF "%5"==""

You see that there are (double) quotes at both sides of the '==' sign.

In other words, I would replace your line by:

if '%%outputfromthiswmiccommand%%' == 'No Instance(s) Available.' ....
Share:
12,260
Kevin
Author by

Kevin

Updated on June 14, 2022

Comments

  • Kevin
    Kevin almost 2 years

    I created a batch script which runs WMIC to get the PID from a specific process. If I open the bat script I see 'ProcessID 1234' or 'No Instance(s) Available.' in the command window.

    I want to create an IF statement where I check if the output from this command was an PID or No Instance(s) Available.. Which variable can I use for this?

    My code:

    wmic process where "Name like '%%cmd.exe%%' and CommandLine like '%%WatchdogStarter.bat%%'" get processid
    if %%outputfromthiswmiccommand%% == 'No Instance(s) Available.' ....
    
  • mojo
    mojo over 8 years
    ++ Language-neutral: ignoring this makes scripts very fragile over the course of time