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!!
Script to email schedule task results
Moderator: DosItHelp
Re: Script to email schedule task results
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:
Of course you have to customize the value.
Regards
aGerman
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
Re: Script to email schedule task results
Thanks! That runs wonderfully and now seems like an obvious solution that I should've figured out.
Thanks again!
Thanks again!