Azure Backup Job Status Email Report: PowerShell Script


Al shows us how a simple PowerShell script can provide email reports for VM backups in Microsoft Azure

Microsoft’s cloud offering via Azure is vast. The amount of services and applications available on the Microsoft Azure platform is ever growing and ever changing. One of the services we’ve found to be great in terms of price and ease of use is the Recovery Services Vaults (backup and replication to Azure).

However, as Azure is changing daily, some functionality in certain datacentre locations aren’t available. I found this out when I configured a new recovery services vault with a new backup job for an Azure Virtual Machine and then realised that email notifications currently weren’t available in the UK South datacentre.

I had a look at these reports in another recovery services vault and noticed they were also basic and as we have our own bespoke backup monitoring solution for all our backups, I needed something I could customise heavily. As such, I started considering PowerShell.

I admit I’m no PowerShell expert and everything I know has either been self-taught from various TechNet articles, research and various use over the past three years. I’m not a developer or fluent in any programming languages (and probably never will be!) However, being able to use basic functions within certain languages whether this be bash, PowerShell, Python or PHP does come in very handy and can save a lot of time by automating reparative tasks.

As such, the below PowerShell script may not be perfect or formatted well, however it does the basic job I needed and hopefully others will find this useful. Basically, this PowerShell script runs daily via a scheduled task, connects to the Azure subscription, returns the status of a specific backup job and then sends this information in an email. This guide presumes you have a good understanding of PowerShell and an SMTP server to send emails (Gmail etc. can be used)

Here’s how it’s done:

First, create a folder to store the credentials and the script. In this example, we’ll use C:Scripts
You’ll then need to save 2 sets of credentials as an encrypted XML to authenticate to your SMTP server to send the email and another set to login to Azure.

Get-Credential | Export-Clixml C:ScriptsSMTPCreds.xml
Get-Credential | Export-Clixml C:ScriptsAzureCreds.xml

For the Azure credentials, I’d recommend creating a user within your Azure AD that only has permissions to read the Azure Recovery Services Vault data. More information on roles can be found here.
Create a new PowerShell script within C:Scripts and edit this with PowerShell ISE or similar.

Add some variables (these should be self-explanatory)

# Set variables
$azCreds = import-clixml c:scriptsAzureServerBackupReportcreds.xml
$smtpCreds = import-clixml c:scriptsAzureServerBackupReportsmtpCreds.xml
$smtpServer = " "
$emailTo = ""
$emailFrom = ""

You then need to define two functions that we’ll use later.

# Backup completed function
function backupCompleted {
send-mailmessage -smtpserver $smtpServer -to $emailTo -from $emailFrom -Subject "Backup job on Server01 has completed" -credential $smtpCreds -body $body

# Backup failed function
function backupFailed {
send-mailmessage -smtpserver $smtpServer -to $emailTo -from $emailFrom -Subject "Backup job on Server01 has failed" -credential $smtpCreds -body $body

Next, set this to login to Azure with your appropriate subscription ID which contains the recovery services vault and the backup job

# Login to Azure
login-azurermaccount -credential $creds -SubscriptionId 00000000-11111-2222-3333-444444444444

If you’re unsure of the subscription ID, you can find this by logging into Azure via PowerShell (as above without -SubscriptionID and running Get-AzureRMSubscription

Now store the recovery services vault as a variable using the name of the recovery services vault.
# Select RSV
$vault = Get-AzureRMRecoveryServicesVault -Name "Resolve-RSV"

If you’re unsure, you can run Get-AzureRMRecoveryServicesVault
Set the context of the recovery services vault to the one above you stored as a variable.
# Set RSV context
Set-AzureRmRecoveryServicesVaultContext -Vault $vault

Store the backup job as a variable using the workload name to select the appropriate backup job. Use Get-AzureRMRecoveryServicesBackupJob | Select Name to list all the jobs.
# Select Server01 AzureVM backup job
$backupJob = Get-AzureRMRecoveryServicesBackupJob | where {$_.WorkloadName -eq "Server01"}

Set the message body variable to the output of the above command
# Set message body to backup job info
$body = $backupJob | out-string

Now a simple If statement to check the output of the backup job and then run one of the earlier defined functions depending on the status

# Check and set status
if ($backupJob.Status -eq "Completed") {
} else {

Save your newly created PowerShell script. Run this manually to test and if successful, you can then add this to a simple scheduled task to run on a daily basis once your backup job has finished.

let's start the ball rolling

Fill in the form or use the contact details below and we’ll get our expert team to put together a package that’s personal to your business.
0114 299 4050