I am trying to find a running process and kill it using PSLIST and PSKILL however, I'm unable to get the errorlevel to set properly. Depending on the way I do it, it gets stuck on either 0 or 1. I originally had the code working the Taskkill and Tasklist commands, but the code has to run on Windows 2000 as well as XP.
I also have notepad++ running to edit the batch file. The following code doesn't differentiate between notepad++ or notepad.exe.
@echo off
reg.exe ADD "HKCU\Software\Sysinternals\PsKill" /v EulaAccepted /t REG_DWORD /d 1 /f >NUL
reg.exe ADD "HKCU\Software\Sysinternals\PsList" /v EulaAccepted /t REG_DWORD /d 1 /f >NUL
rem just to see output of pslist
PSLIST "notepad" 2>NUL
ECHO.
ECHO.
PSLIST "notepad" 2>NUL | FIND /I /N "notepad"
echo The error level is %errorlevel%
IF %errorlevel% EQU 0 (
ECHO Notepad is running and will be terminated.
ECHO.
PSKILL "notepad.exe" 2>NUL
)
IF %errorlevel% EQU 1 (
ECHO Notepad was not running.
ECHO Starting Notepad now...
ECHO.
start "" "notepad.exe"
)
Pause
EXIT
The above code gets stuck on 0. When I change the line with FIND command to FINDSTR as PSLIST "notepad" 2>NUL | FINDSTR /I /N "notepad.exe"
then it gets stuck on 1.
Is there a way to get the PSLIST and FIND or FINDSTR command to return the correct errorlevel with an exact match?