Azure Backup Vault Status report notification via email
After not finding a solution online for Azure Backup Vault reports I #developed this script.
Script by De Jager Laubscher djlmeister@gmail.com and revised by #Theodore Seroadi
Import-module Azure
Put Your Azure Logon details below
$MyAzureName = "joeblogs@blogs.com";
$MyAzurePassword = ConvertTo-SecureString 'YourPasswordHere' -AsPlainText -Force;
$AzureRMCredential = new-object -typename System.Management.Automation.PSCredential -argumentlist $MyAzureName, $MyAzurePassword
Add-AzureRMAccount -Credential $AzureRMCredential
Switch-AzureMode AzureResourceManager
cls
Set the backup vault name and number of days you want to report Backups for
Put the name of your Azure Backup Vault Below
$backupvaultname = "Your Backup Vault name"
$numberofdays = 2
Initialize variables
$DAILYBACKUPSTATS = @()
$backupvault = Get-AzureRMBackupVault -Name $backupvaultname
$enddate = ([datetime]::Today).AddDays(1)
$startdate = ([datetime]::Today)
Put the details of your Email setup below
$smtpServer = "IP of your SMTP Server"
$smtpFrom = "Backupvault@blogs.com"
$smtpTo = "support@blogs.com"
$messageSubject = "Azure Backup Vault Report"
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $True
for( $i = 1; $i -le $numberofdays; $i++ )
{
# We query one day at a time because pulling 2 days of data might be too much
$dailyjoblist = Get-AzureRMBackupJob -Vault $backupvault -From $startdate -To $enddate -Type AzureVM -Operation Backup
Write-Progress -Activity "Getting job information for the last $numberofdays days" -Status "Day -$i" -PercentComplete ([int]([decimal]$i*100/$numberofdays))
foreach( $job in $dailyjoblist )
{
#Extract the information for the reports
$message.Body =
$newstatsobj = New-Object System.Object
$newstatsobj | Add-Member -type NoteProperty -name Date -value $startdate
$newstatsobj | Add-Member -type NoteProperty -name VMName -value $job.WorkloadName
$newstatsobj | Add-Member -type NoteProperty -name Duration -value $job.Duration
$newstatsobj | Add-Member -type NoteProperty -name Status -value $job.Status
$details = Get-AzureRMBackupJobDetails -Job $job
$newstatsobj | Add-Member -type NoteProperty -name BackupSize -value $details.Properties["Backup Size"]
$DAILYBACKUPSTATS += $newstatsobj
}
$enddate = $enddate.AddDays(-1)
$startdate = $startdate.AddDays(-1)
Sets the columns
$a = "<style>"
$a = $a + "BODY{background-color:white;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: Purple;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:LightSeaGreen}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:WhiteSmoke}"
$a = $a + "</style>"
}
Formats and send the message via SMTP
$message.Body = $DAILYBACKUPSTATS | ConvertTo-Html -Head $a
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($message)
2 comments
-
Kris commented
I posted this feedback in script center at https://gallery.technet.microsoft.com/scriptcenter/site/requests/Azure-Backup-Vault-Status-report-notification-via-email-c8446e82
Would be great if this is made as a gallery script in Azure automation. Helps lot of customers. -
Have you thought about posting this content under Script Center in the Azure category? https://gallery.technet.microsoft.com/scriptcenter/site/search?f%5B0%5D.Type=RootCategory&f%5B0%5D.Value=WindowsAzure&f%5B0%5D.Text=Windows%20Azure
Would be great for other users to take advantage of this content and it will be more discoverable there.