Skip to content

Instantly share code, notes, and snippets.

@tillig
Last active February 1, 2024 06:13
Show Gist options
  • Save tillig/9c818bb58c16a1b4b2dbada6fc0c0e3c to your computer and use it in GitHub Desktop.
Save tillig/9c818bb58c16a1b4b2dbada6fc0c0e3c to your computer and use it in GitHub Desktop.
Send Email Alert on Windows Backup

This allows you to use the Windows Task Scheduler to send an email via PowerShell script when a Windows Backup succeeds or fails.

  • Put the two PowerShell scripts in a secure folder that only the administrator can access. They will have credentials to send email in them.
  • Update the placeholders in each script to allow email to send. Run the scripts to ensure the email goes out.
  • Create a scheduled task that is triggered on an event from the event log. Set up a custom filter and use the backup-failed-filter.xml as the filter. This catches all the backup failure events and will trigger the script. Attach that to the Send-BackupFail.ps1.
  • Create a scheduled task that is triggered on an event from the event log. Attach Send-BackupSuccess.ps1 to that.
    • Log: Microsoft-Windows-Backup/Operational
    • Source: Backup
    • Event ID: 4
<QueryList>
<Query Id="0" Path="Microsoft-Windows-Backup">
<Select Path="Microsoft-Windows-Backup">*[System[Provider[@Name='Microsoft-Windows-Backup'] and (EventID=5 or EventID=7 or EventID=8 or EventID=9 or EventID=17 or EventID=22 or EventID=49 or EventID=50 or EventID=52 or EventID=100 or EventID=517 or EventID=518 or EventID=521 or EventID=527 or EventID=528 or EventID=544 or EventID=545 or EventID=546 or EventID=561 or EventID=564 or EventID=612)]]</Select>
</Query>
</QueryList>
$From = "from@gmail.com"
$To = "to@gmail.com"
$Subject = "SERVER Backup FAILED"
$Body = "The most recent backup for SERVER was NOT successful. Check the Windows event log for details."
$Password = "yourpasswordhere" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sender@gmail.com", $Password
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer "smtp.gmail.com" -port 587 -UseSsl -Credential $Credential
$From = "from@gmail.com"
$To = "to@gmail.com"
$Subject = "SERVER Backup Success"
$Body = "The most recent backup for SERVER was successful."
$Password = "yourpasswordhere" | ConvertTo-SecureString -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "sender@gmail.com", $Password
Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer "smtp.gmail.com" -port 587 -UseSsl -Credential $Credential
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment