View unanswered posts | View active topics It is currently 24 Jul 2014 04:39



Post new topic Reply to topic  [ 4 posts ] 
Help, Bat file to monitor scheduled tasks. 
Author Message

Joined: 10 May 2012 14:16
Posts: 3
Post Help, Bat file to monitor scheduled tasks.
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.


10 May 2012 14:34
Profile

Joined: 10 May 2012 14:16
Posts: 3
Post Re: Help, Bat file to monitor scheduled tasks.
I have figured out the duplicates, but still am working on how to ignore scheduled tasks that are disabled... will continue trying stuff.


10 May 2012 18:04
Profile

Joined: 10 Feb 2012 02:20
Posts: 4015
Post Re: Help, Bat file to monitor scheduled tasks.
This is not fully tested - I don't have failed tasks for it to check.

Code:
@echo off
setlocal enableDelayedExpansion

for /f "tokens=1,* delims=:" %%a in ('
schtasks /query /v /fo:list ^|findstr /i "Result: TaskName: State:"
') do (
if "%%a"=="TaskName" set "tn=%%b"
if "%%a"=="Last Result" set "lr=%%b"
if "%%a"=="Scheduled Task State" set "sts=%%b"

if defined sts echo !lr:~-3!, !sts:~-8!, "!tn:~29!"

if defined sts (
if !lr:~-3! GTR 0 if "!sts:~-7!"=="Enabled" (
>e:\scripts\failed.txt echo Task Name failed="!tn:~29!", Task Result=!lr:~-3!
start blat e:\scripts\failed.txt -to email@address.org -s "There is a failed scheduled task on Vassar"
)
set tn=&set lr=&set sts=
)
)


10 May 2012 19:26
Profile

Joined: 10 May 2012 14:16
Posts: 3
Post Re: Help, Bat file to monitor scheduled tasks.
That works perfectly, Thanks a ton! You deserve cookies.


10 May 2012 20:00
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.