Batch: Kill Zombie Tasks

@echo off

rem TMstatus.bat set zombie vars to full or partial task name to test remove from Task Manager
Demonstrates (a) Timestamps (b) FOR parsing of delimited fields and (c) IF logical comparisons
Warning: Replace 'Zombie1' and 'Zombie2', currently set to kill CHROME and APPLEUPDATE


setlocal
set zombie1=firef
set zombie2=SoftwareUpdate
set hasargument=no_argument
if not "%1"=="" set hasargument=%1
:maketimestamp
 

set xtime=%time::=%
set xtime=%xtime:.=_%
set xdate=%date: =_%
set xdate=%xdate:/=_%
rem set xdate=%xdate:~4%
set xtime=%xtime:~0,-3%
rem set timestamp=%xdate%_%xtime%
set timestamp=%xdate%
rem set newdir=_hold_%timestamp%
set outputfile=taskman%xdate%.log 

:randomfile
set randfile=delete%random%.txt
TASKLIST /NH /FI "IMAGENAME eq %zombie1%*"
TASKLIST /NH /FI "IMAGENAME eq %zombie2%*" 

TASKLIST /NH /FI "IMAGENAME eq %zombie1%*" >> %randfile%
TASKLIST /NH /FI "IMAGENAME eq %zombie2%*" >> %randfile% 

for /f "tokens=3" %%f in ('find /i /c "%zombie1%" %randfile%') do set whatcount1=%%f
for /f "tokens=3" %%g in ('find /i /c "%zombie2%" %randfile%') do set whatcount2=%%g
echo %timestamp% %0 count of %zombie1% is: %whatcount1%
echo %timestamp% %0 count of %zombie1% is: %whatcount2% 

echo %timestamp% %0 %hasargument% count of %zombie1% is: %whatcount1% >> %outputfile%
echo - %timestamp% %0 %hasargument% count of %zombie2% is: %whatcount2% >> %outputfile%
rem if %whatcount% GTE 2 TASKKILL /S system /U username /P password /FI "IMAGENAME eq note*"
if %whatcount1% GEQ 1 TASKKILL /FI "IMAGENAME eq %zombie1%*" >> %outputfile%
if %whatcount2% GEQ 1 TASKKILL /FI "IMAGENAME eq %zombie2%*" >> %outputfile% 

DEL %randfile%
:skip
 

ECHO FOR MORE DETAILS try command line: TASKLIST /M /FI "IMAGENAME eq SubSys*" 

if "%1"=="" echo end of %0
if "%1"=="" pause 

endlocal