PowerShell script to report last successful full backup of Exchange 2007

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 = "&nbsp;&nbsp;&nbsp;&nbsp;"
$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)

7 thoughts on “PowerShell script to report last successful full backup of Exchange 2007

  1. Hi ..I have tried this script , but it is throwing error.Not able to send email.. Is it possible to modify the script and write the out put to a .html ( or another file) instead of sending email?

  2. Cool script, but it seems for me to report all the mailboxdatabases of each server more than once. if we have three stores with one database in each, it list the multiple store under all the databases.

    S-MX-CORP01MX01
    CORP HO Storage Group
    CORP HO Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:00 a
    CORP HTL Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:13 a
    CORP HTL Storage Group
    CORP HO Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:00 a
    CORP HTL Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:13 a
    CORP Public Folder Storage Group
    CORP HO Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:00 a
    CORP HTL Mailbox Database Last Backup Started At: Monday, 11/01/2010 10:13 a

  3. I got this working and it looks good but it does not seem to report on incremental bu’s?! We do fulls on the weekend and incrementals during the week.

  4. That is true. When I wrote this script, only daily fulls were being run. It is more common these days to supplement fulls with incrementals (or differentials) because of VSS. It would be easy enough for you to modify the script to simply look at LastIncrementalBackup (or SnapshotLastIncrementalBackup), but using a combination of backup types does complicate it so that it will require more than a simple modification of a line or two in the script. There are several ways I can see to report on what you want: Factor the day of the to determine which backup type to report on; report on both types of backups all the time, using the current day of the way to determine which backup type should be flagged if over X hours old; or perhaps the LastCopyBackup property will make it easier. I couldn’t find what data is reported in that property, though, to know if it will help. I thought maybe it would report the last backup, regardless of the type, but I have a feeling that isn’t what it is. I can’t look at my work installation because we don’t do backups in Exchange 2010, instead using multiple copies in a DAG and the use of a lag copy. While searching for some of this data, I came across several scripts for reporting backups of all the types. I don’t know if they include logic for flagging backups that haven’t occurred in a timeframe, or if that is why you downloaded my version in the first place. If you need further assistance with specifics of modifying my version of the script, let me know.

Leave a Reply

Your email address will not be published. Required fields are marked *

*