Tuesday, October 31, 2017

Powershell Script to Email an Alert if a Scheduled Task fails

-------------------------------------------------------------
Monitor-ScheduleTasks.ps1
-------------------------------------------------------------

<#
.Synopsis
   Determines which scheduled tasks (if any) have failed in the last 30 minutes 
   and sends an email listing those failures.
.DESCRIPTION
   Determines which scheduled tasks (if any) have failed in the last 30 minutes 
   and sends an email listing those failures.
.USAGE
    Create a scheduled task that has three triggers.  
    The triggers are on Task Scheduler log events 101, 103, and 202.
    Be sure that the scheduled task is running under an account with 
    enough credentials to read the log and to run this script, and 
    is running whether or not that account is logged on.
    Obviously, this script should be the action taken on trigger (program is
    powershell.exe, this script path\filename as parameter)
    Adjust the variables that begin with "$Email" below to be correct for your
    environment.
#>

$EmailUsername = "anonymous"
$EmailPassword = ConvertTo-SecureString -String "anonymous" -AsPlainText -Force
$EmailFrom = "$env:COMPUTERNAME@mydomain.com"
$EmailSMTPServer = "MySMTPServer.Domain.com"

$events = get-scheduledtask | where {$_.State -ne "Disabled"| where {((Get-ScheduledTaskInfo $_).LastTaskResult -ne "0"-and ((Get-ScheduledTaskInfo $_).LastTaskResult -ne "267009"-and((Get-ScheduledTaskInfo $_).LastRunTime -ge ((Get-Date).AddMinutes(-30)) -and ((Get-ScheduledTaskInfo $_).TaskPath -notlike "\Microsoft\*" )) -and $_.TaskName -notlike"User_Feed_Synchronization*"| Select TaskName
$count = ($events.TaskName).Count
$EmailBody = ""
if ($count -ge 1){
    foreach ($event in $events){
        $FailedTaskMessage = "A scheduled task on $env:COMPUTERNAME has failed.`n`r`n`rFailed Task:" + $event.TaskName + "`n`r"
        $EmailBody = $EmailBody + $FailedTaskMessage
    }
}
else{
    $EmailBody = "Task Scheduler Engine reported a failed task.`n`r`n`rAutomation is unable to determine which task failed."
}
$EmailCredentials = New-Object System.Management.Automation.PSCredential($EmailUsername,$EmailPassword)
$EmailSubject = "A scheduled task on $env:COMPUTERNAME has failed"
Write-Output $EmailBody
Send-MailMessage -To $EmailTo -From $EmailFrom -Subject $EmailSubject -SmtpServer $EmailSMTPServer -Credential $EmailCredentials -Body $EmailBody