Help, Bat file to monitor scheduled tasks.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Travis.Peterson
Posts: 3
Joined: 10 May 2012 14:16

Help, Bat file to monitor scheduled tasks.

#1 Post by Travis.Peterson » 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.

Travis.Peterson
Posts: 3
Joined: 10 May 2012 14:16

Re: Help, Bat file to monitor scheduled tasks.

#2 Post by Travis.Peterson » 10 May 2012 18:04

I have figured out the duplicates, but still am working on how to ignore scheduled tasks that are disabled... will continue trying stuff.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help, Bat file to monitor scheduled tasks.

#3 Post by foxidrive » 10 May 2012 19:26

This is not fully tested - I don't have failed tasks for it to check.

Code: Select all

@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=
)
)

Travis.Peterson
Posts: 3
Joined: 10 May 2012 14:16

Re: Help, Bat file to monitor scheduled tasks.

#4 Post by Travis.Peterson » 10 May 2012 20:00

That works perfectly, Thanks a ton! You deserve cookies.

Keshav
Posts: 1
Joined: 14 Dec 2017 05:06

Re: Help, Bat file to monitor scheduled tasks.

#5 Post by Keshav » 14 Dec 2017 05:10

Hi, Do I need to make any changes in the script.
Also when I run the script
got below message "Window cannot find balt. Make sure you typed the name correctly"

Code: Select all

@echo off
setlocal enableDelayedExpansion

for /f "tokens=1,* delims=:" %%a in ('
schtasks /query /v /fo:list ^|findstr /i "Result: move: State:"
') do (
if "%%a"=="move" 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" (
>D:\Users\703098641.INDGE\Desktop\script\failed.txt echo Task Name failed="!tn:~29!", Task Result=!lr:~-3!
start balt D:\Users\703098641.INDGE\Desktop\script\failed.txt -to keshav.kalra@ge.com -s "There is a failed scheduled task on Vassar"
)
set tn=&set lr=&set sts=
)
)
Last edited by aGerman on 14 Dec 2017 06:36, edited 1 time in total.
Reason: code tags added

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Help, Bat file to monitor scheduled tasks.

#6 Post by aGerman » 14 Dec 2017 06:43

You need to have blat installed, it has to be in the path environment for your script and it needs to be spelled correctly (you wrote balt instead of blat). Also read the manpage carefully to see what settings you need to be able to use blat.

Steffen

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

Re: Help, Bat file to monitor scheduled tasks.

#7 Post by jfl » 14 Dec 2017 15:54

To monitor something really reliably for a long time, it is much better use a Windows service.
The advantage of a service is that it's basically indestructible. If Windows Update decides in the middle of the night that your PC needs a critical update, and hard-reboots your PC, your service will restart. Even if your service itself crashes, Windows will detect that and restart it automatically.
Writing a Windows service used to be a hard task, as this required compiling a C or C++ or C# program with complex requirements.
Such compiled programs also lacked the flexibility of the simple daemon scripts so common in Linux... or of a batch file as you're doing now, which can be quickly edited and updated.
This is for that very reason that I've developed the PSService.ps1 script a couple of years ago, for a monitoring project at work: We wanted to have a reliable service script in Windows, with the same flexibility we already had in Linux daemon scripts.
I chose PowerShell, as it has direct access to all .NET components required at the core of all services. This is definitely not feasible in Batch.

So (Even though this may be viewed as heresy on this batch forum ;-) I'd recommend that you get the PSService.ps1 sample from github, and rewrite your tasks monitoring script in PowerShell.
Don't be frightened by the large size of that script. Most of it never has to change. All you have to do is to insert your own code in the TimerTick event handler. Also change the period to suit your needs.
If you want more information about how that script works, you can look at that article in MSDN magazine.

Jean-François

Post Reply