Windows 2003 Server: Scheduled Task Failure Notification

jstevens
SilverLounger
Posts: 2328
Joined: 26 Jan 2010, 16:31
Location: Southern California

Windows 2003 Server: Scheduled Task Failure Notification

Post by jstevens »

Is there a way to have the server send an email notification indicating that a scheduled task has failed to run?

Thanks,
John
Regards,
John

JoeP
BronzeLounger
Posts: 1582
Joined: 25 Jan 2010, 02:12

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by JoeP »

You can't do this with native tools in Win2k3. See VB script for monitor the scheduled tasks and send Email notificat - Server Scripting for one way to do this.

Starting with Vista & Server 2008 you can do this with Task Scheduler. See Task Scheduler 2.0 for more information on the updated task scheduler.

Joe
Joe

jstevens
SilverLounger
Posts: 2328
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by jstevens »

Joe,

The code for the 2003 Server works very nicely.

Thank you,
John
Regards,
John

avalosdyn
NewLounger
Posts: 2
Joined: 30 Mar 2016, 15:50

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by avalosdyn »

I'm new to vb scripting and blat.

I tried using the script in the link above but feel that it is not complete. Would somebody please paste a complete script for thsi scheduled task notification using blat please/ Or point me to the right resource so i can figure out this script.

@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
echo blat.exe
)

User avatar
John Gray
PlatinumLounger
Posts: 4764
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by John Gray »

Hello avalosdyn

I'm not quite sure what you want!

I've tried your script on my WIndows 7 Pro 64-bit PC and get a large number of lines, some of which are...

Code: Select all

Task Name=\Microsoft\Windows\AppID\PolicyConverter, Task Result=1
Task \Microsoft\Windows\AppID\PolicyConverter failed
blat.exe
Task Name=\Microsoft\Windows\AppID\VerifiedPublisherCertStoreCheck, Task Result=1
Task \Microsoft\Windows\AppID\VerifiedPublisherCertStoreCheck failed
blat.exe
Task Name=\Microsoft\Windows\Application Experience\AitAgent, Task Result=0
Task Name=\Microsoft\Windows\Application Experience\Microsoft Compatibility Appraiser, Task Result=0
Task Name=\Microsoft\Windows\Application Experience\ProgramDataUpdater, Task Result=0
Task Name=\Microsoft\Windows\Autochk\Proxy, Task Result=0
Task Name=\Microsoft\Windows\Bluetooth\UninstallDeviceTask, Task Result=1
Task \Microsoft\Windows\Bluetooth\UninstallDeviceTask failed
blat.exe
Task Name=\Microsoft\Windows\CertificateServicesClient\SystemTask, Task Result=0
Are you asking how to use BLAT? It's ages since I used any of the many versions of BLAT.

Please say a bit more about your requirements.
John Gray

Snaccident - “the unintentional eating of an entire packet of biscuits".

avalosdyn
NewLounger
Posts: 2
Joined: 30 Mar 2016, 15:50

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by avalosdyn »

Hey John thanks for looking into this.
After poking around a little more, i finally got blat to send email. However when I add the rest of the notification script it does not do what I expected it to. I have an old win2003 server that I cant upgrade. And I want to get notified when a task fails. After looking at some more examples of this script I tweaked it a bit. This is what I have so far:

set YourEmailAddress=to@email.com
set SMTPServer=SMTPserver
set PathToScript=c:\scripts
set FromAddress=from@email.com

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 with result %result% > %PathToScript%\taskcheckerlog.txt
blat.exe %PathToScript%\taskcheckerlog.txt -t %YourEmailAddress% -a "Warning! Failed %name% Scheduled Task on %computername%"
-s %SMTPServer% -f %FromAddress% -b "Task %name% failed with result %result% on %computername%"
)


I changed all the parameters that need to be changed (SMTP, my email, from email...) And again, if I just use the blat line of this script, that send me an email. But when I add the other portion of the script it doesn't notify me of a failed task. I have task that I failed manually to test. This is on Windows Server 2003

User avatar
John Gray
PlatinumLounger
Posts: 4764
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: Windows 2003 Server: Scheduled Task Failure Notification

Post by John Gray »

My only real comment would be to replace
if /i %BOL%==Task (
with
if /i "%BOL%"=="Task" (
but that wouldn't cause your problem.

Unfortunately BATch files as Scheduled Tasks are quite difficult to debug, and you will need a lot of ECHOs like
ECHO [This is where we are] >> %PathToScript%\%~n0.log
and
ECHO Errorlevel from [whatever] was %errorlevel% >> %PathToScript%\%~n0.log
at various places in the BATch file.

Is BLAT.EXE in your path or current directory?
John Gray

Snaccident - “the unintentional eating of an entire packet of biscuits".