DosTips.com

A Forum all about DOS Batch
It is currently 25 May 2016 12:50

All times are UTC-06:00




Post new topic  Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 10 May 2012 14:34 
Offline

Joined: 10 May 2012 14:16
Posts: 3
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.


Top
   
PostPosted: 10 May 2012 18:04 
Offline

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


Top
   
PostPosted: 10 May 2012 19:26 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5595
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=
)
)


Top
   
PostPosted: 10 May 2012 20:00 
Offline

Joined: 10 May 2012 14:16
Posts: 3
That works perfectly, Thanks a ton! You deserve cookies.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 4 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 9 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® Forum Software © phpBB Limited