Script to email schedule task results

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ddsdax
Posts: 2
Joined: 05 Jun 2012 10:43

Script to email schedule task results

#1 Post by ddsdax » 05 Jun 2012 14:03

A dostips.com Expert user, foxidrive, helped with a post last year regarding a batch file to check schedules task status for all tasks. The script he posted checks for enabled tasks and sends an email if the task fails. It works GREAT on my server (Windows Server 2008 R2) but there are many built in Windows tasks I want to exclude because I get 14 messages daily. I will address these tasks and their issues but how would I edit the script to only check status of a particular task by name?
I honestly only have one task that is of great importance.

Here is a slightly modified version of the script posted previously:

@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" (
>C:\sendmail\scripts\failed.txt echo Task Name failed="!tn:~29!", Task Result=!lr:~-3!
start c:\sendmail\sendmail.exe -server 192.168.x.x -from recipient@test.org -to recipient@test.org -subject "Veeam Backup Files Copy Mechanism" -body "Server has failed tasks. Please check" -attach C:\sendmail\scripts\failed.txt
)
set tn=&set lr=&set sts=
)
)

Any help would be greatly appreciated!!

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

Re: Script to email schedule task results

#2 Post by aGerman » 05 Jun 2012 14:49

Run
schtasks /query /?
in the cmd prompt. As you can see there is a /TN option where you can define the task name. The entire path is required.
Change that line in your code:

Code: Select all

schtasks /query /v /fo:list /tn "\path\to\scheduled task" ^|findstr /i "Result: TaskName: State:"

Of course you have to customize the value.

Regards
aGerman

ddsdax
Posts: 2
Joined: 05 Jun 2012 10:43

Re: Script to email schedule task results

#3 Post by ddsdax » 05 Jun 2012 15:01

Thanks! That runs wonderfully and now seems like an obvious solution that I should've figured out.

Thanks again!

Post Reply