Windows Server Backup does a reasonably good job of backing up a Microsoft Server. But like any backup it’s good to have some sort of notification about the success or failure of the backup. I first started to use it with SBS Server 2011 and as part of that bundle the server emails a daily report summarizing the health of the server. When we virtualized and moved a number of SBS servers over to Hyper-V hosts the backup chore moved to the host machine. The summaries still arrived but the backup status was no longer part of it. Besides, it would be nice to be able to see in the subject of the email the status of the backup and not have to open the email if all is good.
The workaround that I came up with was to attach a scheduled task to the backup event. If you open up the Computer Management MMC and go to the Event Viewer and drill down through Applications and Service Logs/Microsoft/Windows/Backup/Operational you will find all of the backup events. If you right click on one of the events, say Event ID 4 (finished successfully), one of the menu options is to Attach A Task To This Event. That will take you to a wizard where you can create a task that will give you the option of running a program, sending an email, or displaying a message. The last two options are depreciated starting in Server 2012 so the strategy was to run a PowerShell script that would send an email. All straightforward for Event ID 4 (success) and 7 (warning) but there are many Event ID’s for failure. I started off with Event ID 5
The actions in the scheduled task are
Start a program PowerShell.exe
with arguments -ExecutionPolicy Bypass C:\Bin\Scripts\BackupFailure.ps1
Powershell script BackupFailure.ps1
Send-MailMessage -SmtpServer "mail.starwars.net" -From "JABBA@starwars.net" -To "starfleet@starwars.net" -Subject "JABBA Backup Failure" -Body "Generated by backup event 5"
Now you can modify the triggers to the task and add more event IDs but that gets a bit tedious, what is needed is a generic script that handles all of the events. The issue here is how do you determine the Event ID and pass it as a parameter to the PowerShell script? I did a lot of Googling before I found this https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/ .
The steps to create a generic task that will respond to any backup event.
- Create a new scheduled task in the Task Scheduler MMC.
- In the General section have it Run whether the user is logged on or not, use an account with Administrative rights.
- In the Triggers section Begin the task:
On an event
, Log:Microsoft-Windows-Backup/Operational
, and Source:Backup
. DO NOT pick an Event ID: - In the Actions pick Action:
Start a program
, Program/script:PowerShell.exe
and Arguments:-ExecutionPolicy Bypass C:\Bin\Scripts\BackupResult.ps1 -result $(IDNumber)
The rest of the settings you can leave as default, save the task.
Now the magic. Right click on the new task and export it to XML. Delete the newly created task. Open the XML file in a text editor like notepad and find the Triggers section and add the following just after the Subscription
section.
Making sure that the name “IDNumber” matches the same value that you are passing as the parameter “-result” to the PowerShell script.
Now go back to the Scheduler MMC and import the XML file back in as a new task. Note that from now on you can’t edit the task in the MMC.
Now the actual script. Create a new PS1 file with the same name and path as the one that you are passing to the task, C:\Bin\Scripts\BackupResult.ps1
and copy and paste the following. (Note: even with the code tags Drupal is stripping out some of the elements of the following section, you can see this if you use the view source feature of your web browser. Best to download the attached zip file http://www.wiseoldcat.com/wp-content/uploads/general/Microsoft-Windows-Backup_Generic_Template_sample.zip and use it.)
# Sends email after scheduled backup with status of the backup job
# modify the shcheduled task by exporting xml, adding ValueQueries to get the Event ID
# in the EventTrigger section, and importing the task back in
# <ValueQueries>
# <Value name="IDNumber">Event/System/EventID</Value>
# </ValueQueries>
# </EventTrigger>
# pass the IDNumber as a parameter to the powershell script in the Exec section
# <Exec>
# <Command>PowerShell.exe</Command>
# <Arguments>-ExecutionPolicy Bypass C:\Bin\Scripts\BackupResult.ps1 -result $(IDNumber)</Arguments>
# </Exec>
# https://blogs.technet.microsoft.com/wincat/2011/08/25/trigger-a-powershell-script-from-a-windows-event/
# https://technet.microsoft.com/en-us/library/dd364735(WS.10).aspx
# http://www.wiseoldcat.com/?q=node/29
param (
[int32]$result = 0
)
$MyServer = "JABBA"
$MyDomain = "starwars.net"
$MailServer = $("mail." + $MyDomain)
$SendTo = $("starfleet@" + $MyDomain)
$SendFrom = $($MyServer + "@" + $MyDomain)
$Status = ""
# For testing
#write-host "$MyServer $MyDomain $MailServer $SendTo $Sendfrom $result" ; exit
switch ($result) {
0 {exit} # no parameter passed?
1 {exit} # event when backup starts
14 {exit} # event 14 always happens at end
4 {$status = "Successful"}
5 {$status = "Failure - check log"}
7 {$status = "Warning - check log"}
8 {$status = "Canceled"}
9 {$status = "Shadow Copy failed"}
49 {$status = "No backup disk"}
50 {$status = "Not enough space"}
100 {$status = "Schedule is off"}
561 {$status = "No backup disk"}
default {$status = "General failure - check log"}
}
Send-MailMessage -SmtpServer "$MailServer" -From "$SendFrom" -To "$SendTo" -Subject "$MyServer Backup Result: $Status Code: $result" -Body "Generated by backup event $result"
Of course change the variables to match those appropriate for your network. If you are dealing with a SMTP server that requires authentication you will have to add to the Send-MailMessage command. Google something like “Send-MailMessage credentials”. I found it easier to set up a new Exchange send connector that allowed anonymous senders from the servers IP address.
I’ve rolled this out to a couple of servers and of course I have found some bugs. On one server (and only one) the subject line gets corrupted to ?us-ascii?Q?JABBA Backup Result: No event ID - testing? Code: 0?=
. That was fixed by adding -Encoding ([System.Text.Encoding]::UTF8)
to the Send-MailMessage command. There are also a couple of EventIDs in the listing above that are not in the zip file (yet). I found that on rare occasions I wasn’t getting a message. This was because two events happened one after another. In the scheduler properties for the task under Settings/If the task is already running – set it to Queue a new instance.
Feel free to customize, all of the backup events can be found at https://technet.microsoft.com/en-us/library/dd364735(WS.10).aspx. The EventID is not the only parameter that you can pick out and pass.