Azure: Email a Backup Report with PowerShell and Office365

This PowerShell snippet compiles a daily report of backup jobs on all the Recovery Service Vaults within the current subscription. It then uses the Office 365 SMTP server to mail this report out to chosen recipients - if you’re not using O365 then just change the SMTPServer, Port, and UseSSL arguments as appropriate in the Send-MailMessage cmdlet.

1$Body=foreach ($RSV in Get-AzRecoveryServicesvault) {
2  Get-AzRecoveryServicesBackupJob -VaultID $RSV.ID -Operation "Backup" -From ((Get-Date).AddDays(-1).ToUniversalTime()) |
3    Select-Object WorkloadName,Operation,Status,StartTime,EndTime,Duration
4}
5$Body= "<h1>Daily Azure Backup Report: " + (Get-AzSubscription).Name +"</h1><p><code>" + ($Body | ConvertTo-HTML)+"</code></p>"
6Send-MailMessage -BodyAsHTML $Body -From "username@mycompany.onmicrosoft.com" `
7   -To "myalerts@mycompany.com" -SmtpServer smtp.office365.com -Port 587 `
8   -Subject "Azure Backup Report" -UseSsl `
9   -Credential (Get-Credential -Message "Office 365 credentials")

If the email should go to multiple recipients then comma separate the list as follows:

1Send-MailMessage -To @("myalerts@company.com","sysadmin@mycompany.com")

Obviously to automate this you’ll need to feed the credentials in, using whatever secure platform you have available, rather than prompting for them in the script. The resulting email looks something like this:

There’s plenty of scope for customisation of the email - the style and look of it can be changed by manipulating the HTML that’s generated in the snippet and the information included can be changed by modifying the Select-Object parameters.