How to setup email alerts for when Windows Azure Backup fails

I have started using Windows Azure Backup, I think it's great, but seems
to be lacking one major feature. When a backup fails, by default it
fails silently. I am not aware of a way to get alerts of failed jobs.

I decided to write my own PowerShell script to perform this for me.

Disclaimer: I am not a PowerShell expert. If you wish to use this script in production, then I would look through it with a lot of detail.

The script is very simple it does the following:

  • Loops through a list of servers and checks the last backup job.
  • If the job did not complete successfully or is over 24 hours old, it will email you.
  • It does not support different authentication per server.

I set this as a Scheduled Task within Windows and it runs every few
hours.

Here is the script:

Function GetLatestJob($serverName)
{
    $pw = convertto-securestring -AsPlainText -Force -String <ENTER PASSWORD HERE>
    $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "<ENTER USERNAME HERE>",$pw

    $session = New-PSSession -ComputerName $serverName -Credential $cred
    $script = {
        $returnData = @()
        foreach($job in Get-OBJob -Previous 1)
        {
            $returnData += New-Object PSObject -Property @{
                starttime = $job.JobStatus.StartTime
                state = $job.JobStatus.JobState
            }
        }
        $returnData
    }

    $returnList = Invoke-Command -Session $session -ScriptBlock $script    

    Remove-PSSession $session
    return $returnList
}

function Send-Email($server, $msgBody){

    $smtpServer = "<ENTER SMTP SERVER HERE>"

    $msg = New-Object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)

    #Email structure 
    $msg.From = "<FROM ADDRESS>"
    $msg.ReplyTo = "<REPLY TO ADDRESS>"
    $msg.To.Add("<TO ADDRESS>")
    $msg.subject = "Backup Failed - $server"
    $msg.body = $msgBody

    #Sending email 
    $smtp.Send($msg)    
}

Try
{

    $backupServers = @("<SERVER1 NAME>", "<SERVER2 NAME>")

    foreach($server in $backupServers)
    {
        $lastBackupJobFailed = $False
        $latestJob = GetLatestJob $server
        $latestJob | ForEach-Object {Write-Host $_.starttime $_.state}

        $timeSpanSinceBackupJob = New-TimeSpan -Start $latestJob.starttime -End (Get-Date)

        if ($timeSpanSinceBackupJob.Days -gt 0)
        {
            Send-Email $server "Error - last backup was over a day ago."
        }

        $latestJobState = $latestJob.state.Value
        $latestJobStartTime = $latestJob.starttime

        if ($latestJobState -ne "Completed")
        {
            Send-Email $server "Error - last backup status: $latestJobState - $latestJobStartTime"
        }
    }
}
Catch [system.exception]
{
    Send-Email "Failed" "The backup script failed $error[0]"
}

As I mentioned, I am not a PowerShell expert, so if anyone has any
improvements they would be greatly appreciated.