PowerShell script to report last successful full backup of Exchange 2007
Edit: The inline code in this post is not the latest version of the script. Get the latest version from the downloads page.
This script is a port of my original backup report that was written in VBScript. That script reports on both 2003 and 2007 servers, but lacked some of the features that I wanted to put in. PowerShell natively supports date-awareness, which makes it much easier to add the number one feature I wanted to add: highlighting servers that haven’t had backups since a specified period of time.
Because I am using the native Exchange cmdlets instead of WMI or CDOEXM, this only reports on Exchange 2007 servers. I figure accommodating both is more work than it is worth, so I just modified my VBScript version to not include any server in the Exchange 12 admin group and I have both run every day until my migration to 2007 is complete.
The script reports the last successful full backup of any Exchange 2007 server with the mailbox role installed. It checks for the presence of storage groups and databases within them. It notes if a backup is currently in progress, as well as if a backup has never completed. If a backup has not completed in the last 72 hours (modifiable), it is highlighted in red so it is easy to spot. If a backup is less than the defined number of hours old, I use the Marlett font to display a green checkmark. This allows for a checkmark without having to reference an external image or embed one. Lastly, the report is emailed. The script is shown below, but you can also just download it.
#Last Backup Report for Exchange 2007 servers
#Version 1.0 - 7/9/08
#--------------------------------------------
#Begin customization-------------------------
$SmtpServer = "server.domain.com" #Enter FQDN of SMTP server
$SmtpFrom = "Exchange Backups <exchangebackupreport@domain.com>" #Enter sender email address
$SmtpTo = "user1@domain.com","user2@domain.com" #Enter one or more recipient addresses in an array
$SmtpSubject = "Exchange 2007 Last Backup Report" #Enter subject of message
$iNomHours = "72" #Enter number of hours since last backup that requires attention
#End customization---------------------------
$date = Get-Date
$sSpace = " "
$sOutput = "<table>"
#Checkmark to indicate last backup within nominal time
$sCheckMark = "<span style=""font-family: Marlett; color: green; font-size: 14pt; font-weight: bold"">a</span>"
#Retrieve Exchange servers with mailbox role
$ExServer = Get-ExchangeServer | where {$_.IsMailboxServer -eq $True} | Sort-Object Name
Foreach ($server in $ExServer)
{
$sOutput += "<tr><td><font size=2><u><b>$server</b></u></font></td><td></td></tr>"
#Retrieve storage groups for a given server
$StorageGroup = $server | Get-StorageGroup | Sort-Object Name
#Check for absence of any storage groups
If (($StorageGroup | Measure-Object Name).Count -eq $null)
{
$sOutput += "<tr><td><font size=2>" + $sSpace + "No storage groups present.</font></td></tr>"
}
Else
{
Foreach ($sg in $StorageGroup)
{
$sOutput += "<tr><td><font size=2>" + $sSpace + $sg.Name + "</font></td></tr>"
#Retrieve mailbox databases for a given storage group
$MailboxDatabase = $StorageGroup | Get-MailboxDatabase -Status | Sort-Object Name
#Check for absence of any databases in storage group
If (($MailboxDatabase | Measure-Object Name).Count -eq $null)
{
$sOutput += "<tr><td><font size=2>" + $sSpace + $sSpace + "No mailbox stores present.</font></td></tr>" }
Else
{
Foreach ($db in $MailboxDatabase)
{
$sBackupRunning = ""
#Note if backup is currently running
If ($db.BackupInProgress -eq $true)
{$sBackupRunning = "<font size=2 color=blue>(Backup In Progress)</font>"}
#Determine if backup has ever completed
If ($db.LastFullBackup -ne $null)
{
$sBackupDay = $db.LastFullBackup.get_DayofWeek()
$sBackupDateTime = $db.LastFullBackup.ToString("g")
#Flag if last completed backup started longer than defined variable
If (($date - $db.LastFullBackup).TotalHours -gt $iNomHours)
{
$sLastBackup = "<font size=2>Last Backup Started At: <font color=red>" + $sBackupDay + ", " + $sBackupDateTime + "</font></font>"
}
Else
{
$sLastBackup = "<font size=2>Last Backup Started At: " + $sBackupDay + ", " + $sBackupDateTime + " </font>" + $sCheckmark
}
}
Else
{
$sLastBackup = "<font size=2>No full backup has completed yet.</font>"
}
$sOutput += "<tr><td><font size=2>" + $sSpace + $sSpace + $db.Name + " </font></td><td>" + $sLastBackup + $sBackupRunning + "</td></tr>"
}
}
}
}
}
$sOutput += "</table>"
#Email results
$SmtpClient = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage
$SmtpClient.Host = $SmtpServer
$MailMessage.From = $SmtpFrom
Foreach ($address in $smtpTo)
{$MailMessage.To.Add($address)}
$MailMessage.Subject = $SmtpSubject
$MailMessage.IsBodyHTML = $true
$MailMessage.Body = $sOutput
$SmtpClient.Send($MailMessage)
