Azure Replication Status Report – Email Notification

Recently I was given the task to monitor the replication status of a large number of servers, I really didn’t want to be going via the classic console and clicking inside every replication group just to make sure things were rolling smoothly. It got me thinking about how I could automate this task using Azure Automation Run books. I started digging around and came across a post where a gentleman had written a Powershell script to deliver the daily backup jobs via email. It was not exactly what I wanted so I took that script and modified it to deliver the information I required. First step is to create a runbook in Azure Give the runbook a useful name and choose the powershell option as the runbook type For the runbook to create a connection you will need to add the following code at the start of your script, if you have not setup an Azure RunasAccount you can follow these instructions – Authenticate runbooks with an Azure Run As account Be sure to populate your runas account next to $connectionName
$connectionName = "AzureRunAsConnection"
$SubId = Get-AutomationVariable -Name 'SubscriptionId'
try
{
   # Get the connection "your azure runas account name here"
   $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

   "Logging in to Azure..."
   Add-AzureRmAccount `
     -ServicePrincipal `
     -TenantId $servicePrincipalConnection.TenantId `
     -ApplicationId $servicePrincipalConnection.ApplicationId `
     -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
   "Setting context to a specific subscription"     
   Set-AzureRmContext -SubscriptionId $SubId              
}
catch {
    if (!$servicePrincipalConnection)
    {
       $ErrorMessage = "Connection $connectionName not found."
       throw $ErrorMessage
     } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
     }
}
Next Add the code below This is the workhorse that produces the report, you will need to populate your SMTP settings
$DAILYSTATS = @()

$SmtpUser = "username"
$SmtpPass = "password"
$smtpFrom =  "from address"
$SmtpPort = "587"
$smtpServer = "smtp.office365.com"
$smtpTo = "sender email address"
$messageSubject = "Daily ASR Report" , $MsgstartTime
$message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
$message.Subject = $messageSubject
$message.IsBodyHTML = $True

$vault = Get-AzureRmSiteRecoveryVault -Name "Your Recovery Vault Name Here"  

$vaultsettingsfile = Get-AzureRmSiteRecoveryVaultSettingsFile -Vault $vault
Import-AzureRmSiteRecoveryVaultSettingsFile -Path $vaultsettingsfile.FilePath

$container = Get-AzureRmSiteRecoveryProtectionContainer
$results = Get-AzureRmSiteRecoveryReplicationProtectedItem -ProtectionContainer $container 

foreach( $status in $results )
{

$message.Body =
$newstatsobj = New-Object System.Object

$newstatsobj | Add-Member -type NoteProperty -name Server -value $status.FriendlyName
$newstatsobj | Add-Member -type NoteProperty -name Group -value $status.PolicyFriendlyName
$newstatsobj | Add-Member -type NoteProperty -name Source -value $status.PrimaryFabricFriendlyName
$newstatsobj | Add-Member -type NoteProperty -name State -value $status.ProtectionState
$newstatsobj | Add-Member -type NoteProperty -name Health -value $status.ReplicationHealth




$newstatsobj | Add-Member -type NoteProperty -name CreationTime -value (($status.ReplicationHealthErrors.CreationTimeUtc)-join"<==>")
$newstatsobj | Add-Member -type NoteProperty -name ErrorCode -value (($status.ReplicationHealthErrors.ErrorCode)-join"<==>")
$newstatsobj | Add-Member -type NoteProperty -name ErrorStatus -value (($status.ReplicationHealthErrors.ErrorLevel)-join"<==>")
$newstatsobj | Add-Member -type NoteProperty -name Message -value (($status.ReplicationHealthErrors.ErrorMessage)-join"<==>")
$newstatsobj | Add-Member -type NoteProperty -name PossibleCauses -value (($status.ReplicationHealthErrors.PossibleCauses)-join"<==>")
$newstatsobj | Add-Member -type NoteProperty -name Action -value (($status.ReplicationHealthErrors.RecommendedAction)-join"<==>")


$DAILYSTATS += $newstatsobj



#Sets the column
$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:White}”
$a = $a + “</style>”

}



$message.Body = $DAILYSTATS | ConvertTo-Html -Head $a
$smtp = New-Object System.Net.Mail.SmtpClient( $SmtpServer , $SmtpPort )
$SMTP.EnableSsl = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential( $SmtpUser , $SmtpPass );
$smtp.Send($message)
You are now ready to test the runbook works Select Test pane Select edit and start the test, the runbook will be queue for an automation worker to pick it up Wait for the runbook to complete. A new email should arrive with a table of results like below Now you just need to publish the runbook and add a schedule You can set the schedule for an hourly drop into your inbox or drop the results into a group inbox.

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Get in touch

Your Name (required)

Your Email (required)

Subject

Your Message

Book a Demo

Your Name (required)

Your Email (required)

Phone Number(Optional)

Ask a Question

Your Name (required)

Your Email (required)

Phone Number(Optional)

Your Message

Book Assessment

Evaluate your modern workplace security posture and validate it against current best practices with a Microsoft Secure Score Assessment, from cubesys

Your Name (required)

Your Email (required)

Phone Number(Optional)