Background

Jocha previously released a very popular mail report-script for Windows Backup. Since the release of Azure Backup we’ve been getting a lot of inquiries regarding the different solutions. One downside with the Azure Backup-agent is that it did not offer mail reports on success/failures.

We’ve built a PowerShell-script that will give you a nice report summary of the last days backup job.

Requirements

– Elevated PowerShell-session.
– At least Windows Management Framework 3.0
– At least Azure PowerShell 1.4

Solution

This PowerShell-script will retrieve the backup jobs from the last 24 hours and send an email with a report of the results.

– Changelog –
2016-05-29 (1.4) Corrected “End Time” in the mail-report.
2016-05-28 (1.3) Streamlined functions and removed unnecessary HTML/CSS.
2016-05-27 (1.2) Now compatible with multiple sources per backup job (Thanks Milan Thaker).
2016-05-19 (1.1) Corrected typos and streamlined variables.
2016-05-17 (1.0) Initial release.

<#
.SYNOPSIS
Azure Online Backup Report via Mail
Written by Jocha AB, https://jocha.se/blog/tech/azure-backup-mail-report-script
.DESCRIPTION
Version 1.4
This script will compile a report of the Azure Backup jobs from the last 24 hours.
.EXAMPLE
To automate this script, setup a scheduled task.
    Name: Azure Backup Email Task
    Description: Notifies backup admin of scheduled backup status
    Action: Start a Program
        Program: Powershell
        Arguments: -Command "C:\Scripts\OBJobReport.ps1" -ExecutionPolicy Bypass
#>

#######################################
#-------- Variables to change --------#

# Set your Company name
$Company = "MyCompany"
 
# Set the recipient/sender email-address
$MailTo = "[email protected]"
$MailFrom = "$Company Backup <[email protected]>"

# SMTP user account password
$MailUser = "MyUser"
$MailPassword = "MyPassword" 

# SMTP Server
$MailServer = "smtpserver.company.com"
 
# SMTP Port
$MailPort = 587
 
# If your server uses SSL, otherwise set to $false
$UseSSL = $true
 
#---- Don't change anything below ----#
#######################################

Try {

Function FormatBytes {
	Param(
		[System.Int64]$Bytes
	)
	[string]$BigBytes = ""
	#Convert to TB
	If ($Bytes -ge 1TB) {$BigBytes = [math]::round($Bytes / 1TB, 2); $BigBytes += " TB"}
	#Convert to GB
	ElseIf ($Bytes -ge 1GB) {$BigBytes = [math]::round($Bytes / 1GB, 2); $BigBytes += " GB"}
	#Convert to MB
	ElseIf ($Bytes -ge 1MB) {$BigBytes = [math]::round($Bytes / 1MB, 2); $BigBytes += " MB"}
	#Convert to KB
	ElseIf ($Bytes -ge 1KB) {$BigBytes = [math]::round($Bytes / 1KB, 2); $BigBytes += " KB"}
	#If smaller than 1KB, leave at bytes.
	Else {$BigBytes = $Bytes; $BigBytes += " Bytes"}
	Return $BigBytes
}

Function Log-BackupItems {
    Param(
        [System.String]$Name,
        [System.String]$Status,
        [System.String]$Start,
        [System.String]$End,
        [System.Int64]$Upload,
        [System.Int64]$Size
    )
    $Item = New-Object System.Object;
    $Item | Add-Member -Type NoteProperty -Name "Name" -Value $Name;
    $Item | Add-Member -Type NoteProperty -Name "Status" -Value $Status;
    $Item | Add-Member -Type NoteProperty -Name "Start Time" -Value $Start;
    $Item | Add-Member -Type NoteProperty -Name "End Time" -Value $End;
    $Item | Add-Member -Type NoteProperty -Name "Uploaded" -Value (FormatBytes -Bytes $Upload);
    $Item | Add-Member -Type NoteProperty -Name "Total Size" -Value (FormatBytes -Bytes $Size);
    Return $Item;
}

Import-module Azure

$Password = ConvertTo-SecureString $MailPassword -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($MailUser, $Password)

$CurrentTime = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$OBScope = (Get-Date).AddDays(-1)
$OBJob = (Get-OBJob -Previous 99).JobStatus | Sort StartTime | Where { $_.StartTime -gt $OBScope }

If ($OBJob.JobState -contains "Failed") { $OBResult = "Failed" }
ElseIf ($OBJob.JobState -contains "Aborted") { $OBResult = "Aborted" }
ElseIf ($OBJob.JobState -contains "Completed") { $OBResult = "Completed" }
ElseIf ($OBJob.JobState -eq $null) { $OBResult = "Missing" }
Else { $OBResult = "Unknown" }

[email protected]()
If ($OBJob.JobState -ne $null) {
    $OBJob | % {
    $OBCount = 0
    foreach($Source in $_.DatasourceStatus.Datasource) {
        $BackupItem = $null
        $BackupItem = Log-BackupItems -Start $_.StartTime.ToString("yyyy-MM-dd HH:mm:ss") -End $_.EndTime.ToString("yyyy-MM-dd HH:mm:ss") -Name $Source.DataSourceName -Status $_.Jobstate -Upload $_.DatasourceStatus.ByteProgress[$OBCount].Changed -Size $_.DatasourceStatus.ByteProgress[$OBCount].Total
        $results += $BackupItem
        $OBCount += 1
        }
    }
}
Else { $results = Log-BackupItems -Start "N/A" -End "N/A" -Name "N/A" -Status "N/A" -Upload 0 -Size 0 }

# Assemble the HTML Report
$HTMLMessage = @"
    $Company Azure Backup Report
    
    

$Company Azure Backup Report

Backup Result: $OBResult
$( $html = $results | ConvertTo-HTML -Fragment $xml=[xml]$html $attr=$xml.CreateAttribute('id') $attr.Value='items' $xml.table.Attributes.Append($attr) | out-null $html=$xml.OuterXml | out-string $html )
"@ $email = @{ SMTPServer = $MailServer UseSSL = $UseSSL BodyAsHtml = $true Port = $MailPort Credential = $Credentials Encoding = ([System.Text.Encoding]::UTF8) To = $MailTo From = $MailFrom Subject = "Azure Backup $OBResult" Body = $HTMLMessage } Send-MailMessage @email } Catch { $email = @{ SMTPServer = $MailServer UseSSL = $UseSSL BodyAsHtml = $true Port = $MailPort Credential = $Credentials Encoding = ([System.Text.Encoding]::UTF8) To = $MailTo From = $MailFrom Subject = "Azure Backup Failed." Body = "The backup script failed to run!" } Send-MailMessage @email }

We hope you’ve found this article helpful, feel free to share it on your social platforms.
Please let us know if you have any questions!