Edit: review Ian Boyd's answer then when you get to using the Email Action, come back here.
Since Ian Boyd's original answer was posted, the Email Action in Task Scheduler was deprecated and eventually removed. But its not difficult to create a Powershell script that will send SMTP emails and run that instead. Here's an example using Powershell and system.net.mail.smtpClient
:
$SmtpClient = new-object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.Host = "mail.yourdomain.com"
$SmtpClient.Port = 587
#$SmtpClient.EnableSsl = $true
$SmtpClient.Credentials = New-Object System.Net.NetworkCredential( "[email protected]", "{pw}" );
$mailmessage.from = ("[email protected]")
$mailmessage.To.add("[email protected]")
$mailmessage.Subject = “Server Alert”
$mailmessage.Body = “Event 103 or 203 detected. This indicates a Scheduled Task has failed.”
$smtpclient.Send($mailmessage)
Edit the details as needed (such as "mail.yourdomain.com" and credentials "[email protected]" and "{pw}"). You might need to monkey with Port
and EnableSsl
.
Save this script as FailedTaskAlert.ps1
in a common folder, such as C:\Users\Public\Documents
.
Now let's wire that up to a Task.
In Action
tab use the Start a Program
action. In Program
, enter "powershell.exe" and in Add Arguments
enter your ps1 file, such as "C:\Users\Public\Documents\FailedTaskAlert.ps1"
I see Hans Folk is also doing this using Send-MailMessage
but I thought another cmdlet variation and an explicit explanation how the Powershell script can be wired to the Task's Action would be helpful to others.
Caveat: make sure that All Task History is enabled in the right hand Actions panel in Task Scheduler otherwise you won't get events logged. If you see the History tab with a "disabled" label, you know this feature is off.