Hi Readers,
We have created a new script which was required by our backup team.
They had below requirements:
- Montor Task Scheduler for failed tasks
- Send email if the task is failed.
- Send the error as attachement
- Send the server name in message subject
- Only monitor tasks that have name snapshot or backup
- Updated 02/28/2014 — to cover the tasks that run for hours
- Updated so that multiple emails for same alert are not received.
So download the attached solution, extract it.
http://gallery.technet.microsoft.com/scriptcenter/Schedule-Task-Monitor-a7c74403
Enter the server names in servers.txt file.
Change the variables accordingly
$hours = “-720” (-720 ensures if the task is running for some hours than also it is covered, you can increse this value upto -1440)
$matching1 = “snapshot”
$matching2 = “backup”
$email1 = “sukhija@labtest.com”
$from = “ScheduleTask_Monitor@labtest.com”
$smtpserver =”smtp.labtest.com”
Schedule the batch file from task scheduler to run every hour.
PowerShell
################################################################################## # Author:Prakash Yadav # Reviewer: Vikas Sukhija # Date: 11/12/2013 # modified:02/28/2013 # Description: Schedule Event Failed script. ################################################################################## ############# Variables################# $date=(get-date).adddays(-1) $hours = "-720" $matching1 = "snapshot" $matching2 = "backup" $email1 = "sukhija@labtest.com" $from = "ScheduleTask_Monitor@labtest.com" $smtpserver ="smtp.labtest.com" #################Define Logs############# $date1 = get-date -format d # replace \ by - $time = get-date -format t $date1 = $date1.ToString().Replace("/", "-") $time = $time.ToString().Replace(":", "-") $time = $time.ToString().Replace(" ", "") $logs = ".\Logs" + "\" + "Powershell" + $date1 + "_" + $time + "_.txt" $emchk1 = ".\Logs" + "\" + "emailcheck" + $date1 + "_.txt" Start-Transcript -Path $logs if((test-path $emchk1) -like $false) { new-item $emchk1 -type file } ##Add servers in text file for which Tasks needs to monitored $server_list= Get-Content .\servers.txt ## Loop thru server List foreach ($i in $server_list) { $schedule = new-object -com("Schedule.Service") $schedule.connect("$i") $tasks = $schedule.getfolder("\").gettasks(0) $Failed = $tasks | where-object{($_.LastTaskResult -ne 0) -and ($_.State -eq 3) -and ($_.Name -match $matching1 -or $_.Name -match $matching2) -and ((get-date).addminutes($hours) -lt $_.LastRunTime)} if ($Failed -eq $Null) { write-host "No task failed in last hour for server $i" } Else { foreach ($ta in $Failed) { $b = $null > ".\Failed.txt" $ta >> ".\Failed.txt" $rcode1 = $ta.Name $rcode2 = $ta.LastRunTime write-host "task $rcode1 failed in last hour for server $i" ########Send Mail with ourPut file as attachments######## ########Change To/From fields accordingly########## $subject ="Task failed at" + "-" +$i + "-"+"Task Name" + "-" + $rcode1 + "-"+"Last Run on" + $rcode2 $cmp=get-content $emchk1 $compare = Compare-Object $cmp $subject -IncludeEqual $compare1=$compare | where{($_.InputObject -eq $subject) -and ($_.SideIndicator -eq "==") } $compare2 = $compare1.InputObject write-host "$compare2" -ForegroundColor green write-host "$subject" -ForegroundColor blue if ($compare2 -ne $subject) { add-content $emchk1 $subject $body = $rcode $message = new-object Net.Mail.MailMessage $smtp = new-object Net.Mail.SmtpClient($smtpserver) $message.From = $from $message.To.Add($email1) $message.body = $body $message.Attachments.Add(".\Failed.txt") $message.subject = $subject $smtp.Send($message) $message.dispose() } else { write-host "email already sent for $subject" } } } } Stop-Transcript #############################################################################################
Regards
Sukhija Vikas