Introduction
In the last article of this series we explored how to handle an ever expanding set of logs and the disk space that these took up. In this article we have a short, but sweet article written to simple report on the backup status of Exchange databases. In some ways this article can also related to the last one because it also has to do with disk space, but in this case the Exchange database logs that should be truncated on a daily basis and not left to expand and fill up a disk. With a simple script we can validate that backups are occurring and logs should also truncated as expected.
Where to Start?
Each mailbox database has a series of properties that related to backups. We can see these properties by simply reviewing one in an Exchange environment:
Get-MailboxDatabase | fl *Backup*
This presents us with a short list of properties from which to choose as important while monitoring backups:
Depending on your backup scheme (full daily backups or full weekly and daily incremental) the script code provided may need to be tweaked. For the sample script below, we are reviewing Exchange Databases for daily backups to validate our backup solution is working. We will thus concentrate on the ‘LastFullBackup’ value on the databases. Now let’s dive into the code.
Script to Check all Databases
Connecting to an Exchange Server
In this script we will assume that a jump or admin box is being used, which means that the Exchange PowerShell module may not be installed. As such, we will run this with regular Windows PowerShell and connect to a remote Exchange Server of our choice.
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://exchange.domain.com/PowerShell/ -Authentication Kerberos Import-PSSession $Session -DisableNameChecking
Keep in mind that ‘exchange.domain.com’ is simply the FQDN (Fully Qualified Domain Name) of the Exchange Server with which we will run our cmdlets from.
List of Exchange Databases
As this script will need to analyze each Exchange Server, we’ll need to pull that list. To make the list cleaner we will select just the name and then sort that list. All of this can be done with one line:
$Databases = Get-MailboxDatabase -Status
Checking Each Database
After getting a list, we can use a Foreach statement to kick off a loop for each database found:
Foreach ($Database in $Databases) {
After entering the loop for a single database, we can then populate some variables with database details:
$DatabaseName = $Database.Name $ExchangeServer = $Database.Server $BackupDate = $Database.LastFullBackup
After pulling the backup date for the last full backup we can then decide how to handle the data found. If the date is empty, the backups have failed or if the date is over a day old, we can also consider it failed as these are daily backups. Processing the data requires this code block:
If ([string]::IsNullOrWhiteSpace($BackupDate)) { $Failed = $True $BackupDate = "Never" } Else { $OneDayOld = $Date.AddDays(-1) If ($BackupDate -lt $OneDayOld) { $Failed = $True } Else { $Failed = $False } }
Producing Results
Once we get a failed of true or false we can display the results
If ($Failed) { Write-Host "Failed: $ExchangeServer,$DatabaseName,$BackupDate" } Else { Write-Host "Success: $ExchangeServer,$DatabaseName,$BackupDate}" }
Sample Results
Conclusion
With this simple script we can pull a list of successful and failed backups and act on the results provided. Additionally, this script could be enhanced to create a report file to be sent in the body of an email to IT in a format like so: