Problem 1: My code abruptly stops with no output in console.
Problem 2: Line 5 of my code doesn't capture the process ID when in the code block, but it does as a standalone command. Could this be because of the set command not working in else blocks?
Here's the goal of the code:
- Check if explorer.exe is running.
- If more than one instance is running, set a variable to multiple.
- Else, if no instances are running, set a variables to not found.
- Else, if one instance is running, dump the memory of explorer.exe.
I expect to grab the process ID of explorer.exe, but the PID is not defined and the program stops at, I believe, line 2. I've inserted pauses after every line of code, and that's where it happens.
for /f "tokens=1,*" %%a in ('tasklist ^| find /I /C "explorer.exe"') do set amount=%%a
if %amount% GTR 1 (
set multiple-explorer= - [ALERT] Multiple instances of explorer.exe found.
) else (
for /F "tokens=1,2" %%i in ('tasklist /FI "IMAGENAME eq explorer.exe" /fo table /nh') do set PID=%%j
if "%PID%" EQU "No" (
set explorer= - [ALERT] explorer.exe not found.
) else (
echo [#] Dumping memory of explorer.exe...
strings2.exe -pid %PID% -nh > explorer.txt
)
)
My goal for this code is also to make it as compact as possible without any goto lines. Any help is appreciated, thanks.