Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

batch-file

cmd

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.' ....
like image 386
Kevin Avatar asked Oct 21 '25 04:10

Kevin


2 Answers

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.

like image 134
Mofi Avatar answered Oct 24 '25 03:10

Mofi


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 worked more or less, 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 WMIC process that was looking up the requested process id.

That's 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!:)

like image 37
Kevin Avatar answered Oct 24 '25 04:10

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!