Help, Bat file to monitor scheduled tasks.
Posted: 10 May 2012 14:34
I'm working on a project where I want a batch file to check status of my scheduled tasks, create a txt file and then e-mail me when one fails. I have it set up and it's working OK, but I need it to ignore disabled tasks. It also is recognising some of my tasks more than one time. Any help would be great.
Here's the batch file:
@echo off
for /F "delims=" %%a in ('schtasks /query /v /fo:list ^| findstr /i "Taskname Result"') do call :Sub %%a
goto :eof
:Sub
set Line=%*
set BOL=%Line:~0,4%
set MOL=%Line:~38%
if /i %BOL%==Task (
set name=%MOL%
goto :eof
)
set result=%MOL%
echo Task Name=%name%, Task Result=%result%
if not %result%==0 (
echo Task %name% failed>failed.txt
start blat e:\scripts\failed.txt -to email@address.org -s "There is a failed scheduled task on Vassar"
)
Here is a sample of the results I get from running the batch file:
E:\Scripts>ScheduledTaskStatusEmail.bat
Task Name=Aloha-Employee Charges to Lawson, Task Result=4
Task Name=Aloha-Employee Charges to Lawson, Task Result=4
Task Name=audit_dat_cleanup, Task Result=0
Task Name=copy onbase log, Task Result=4
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=DFP_Epic_to_BestBill_GPG, Task Result=0
Task Name=DFP_Epic_to_BestBill_GPG, Task Result=0
Task Name=On Demand - Email_results_PreCheck, Task Result=2
I have ommited quite a few, but as you see here, it see's some tasks more than once even though it's only one task. I'm receiving multiple e-mails for the same task that is only listed once.
Any help would be greatly appreciated. Also running on server 2003 R2.
Here's the batch file:
@echo off
for /F "delims=" %%a in ('schtasks /query /v /fo:list ^| findstr /i "Taskname Result"') do call :Sub %%a
goto :eof
:Sub
set Line=%*
set BOL=%Line:~0,4%
set MOL=%Line:~38%
if /i %BOL%==Task (
set name=%MOL%
goto :eof
)
set result=%MOL%
echo Task Name=%name%, Task Result=%result%
if not %result%==0 (
echo Task %name% failed>failed.txt
start blat e:\scripts\failed.txt -to email@address.org -s "There is a failed scheduled task on Vassar"
)
Here is a sample of the results I get from running the batch file:
E:\Scripts>ScheduledTaskStatusEmail.bat
Task Name=Aloha-Employee Charges to Lawson, Task Result=4
Task Name=Aloha-Employee Charges to Lawson, Task Result=4
Task Name=audit_dat_cleanup, Task Result=0
Task Name=copy onbase log, Task Result=4
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=cpm_monthly_upload_GPG_ZIP, Task Result=0
Task Name=DFP_Epic_to_BestBill_GPG, Task Result=0
Task Name=DFP_Epic_to_BestBill_GPG, Task Result=0
Task Name=On Demand - Email_results_PreCheck, Task Result=2
I have ommited quite a few, but as you see here, it see's some tasks more than once even though it's only one task. I'm receiving multiple e-mails for the same task that is only listed once.
Any help would be greatly appreciated. Also running on server 2003 R2.