This script demonstrates a concept, but if you want a more complete working script use Get-DailyBackupAlerts.ps1 instead.

Here is a simple script you can use to check the last backup time for the mailbox databases on an Exchange Server. Run the script from the Exchange Management Shell on a Mailbox Server to check the local server’s databases.

[PS] C:Scripts>.check-dbbackups.ps1
Server: ESP-HO-EX2010B
Current time: 03/27/2011 22:21:39

Database                                Last Full (hrs ago)                     Last Inc (hrs ago)
--------                                -------------------                     ------------------
MB-HO-01                                1                                       n/a
MB-HO-02                                1                                       n/a
MB-HO-Archive-01                        n/a                                     n/a

Here is the script code.

$server = $env:computername

$now = [DateTime]::Now

"Server: " + $server
"Current time: " + $now

$db = get-MailboxDatabase -server $server -status
foreach ($objItem in $db)
{
$lastfull = $objItem.lastfullbackup
	if (!$lastfull)
	{
		$fullago = "n/a"
	}
	else
	{
		$fullago = $now - $lastfull
		$fullago = $fullago.TotalHours
		$fullago = "{0:N0}" -f $fullago
	}

	$lastinc = $objItem.lastincrementalbackup
	if (!$lastinc)
	{
		$incago = "n/a"
	}
	else
	{
		$incago = $now - $lastinc
		$incago = $incago.TotalHours
		$incago = "{0:N0}" -f $incago
	}


$returnedObj = new-object PSObject
$returnedObj | add-member NoteProperty -name "Database" -value $objItem.Name
$returnedObj | add-member NoteProperty -name "Last Full (hrs ago)" -value $fullago
$returnedObj | Add-Member NoteProperty -name "Last Inc (hrs ago)" -value $incago
$returnedObj
}

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for Practical365.com.

Comments

  1. abhishek

    Hello Paul

    This is Abhishek from New Delhi/ India. Your Script works great in our envt. Thanks for this wonderful script

    I am newbie to Powershell, so don’t know much about it, In the Database Backup Alert Email, can you also advise me where to add the field of Last Incremental Backup, Size of databases, space remaining on the drives and threshold of 1 week alert .

    Thanks

    Thanks

    1. Avatar photo
  2. Hesta

    Hi Paul,

    How to check last backup per user mailbox not database mailbox ? could this script can do that ?

    Thank you

      1. Hesta

        Hi Paul,

        I mean how to see the last backup per user mailbox with use this script. it is posibble ?

        Thank You

        1. Avatar photo

          Mailboxes are hosted in databases. Databases are backed up. This script reports on the backup times for databases.

          If you want to know which database a mailbox is located in you can run Get-Mailbox to see that information.

  3. Robert

    This was helpful for me… I wanted the script to run, on a click, and wait for me to read the results before closing…

    Open notepad, paste the code below in…
    Save as something nifty like: “Check Exchange Last Backup.PS1”
    ——————————————————————————————-

    add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn
    $server = $env:computername

    $now = [DateTime]::Now

    “Server: ” + $server
    “Current time: ” + $now

    $db = get-MailboxDatabase -server $server -status
    foreach ($objItem in $db)
    {
    $lastfull = $objItem.lastfullbackup
    if (!$lastfull)
    {
    $fullago = “n/a”
    }
    else
    {
    $fullago = $now – $lastfull
    $fullago = $fullago.TotalHours
    $fullago = “{0:N0}” -f $fullago
    }

    $lastinc = $objItem.lastincrementalbackup
    if (!$lastinc)
    {
    $incago = “n/a”
    }
    else
    {
    $incago = $now – $lastinc
    $incago = $incago.TotalHours
    $incago = “{0:N0}” -f $incago
    }

    $returnedObj = new-object PSObject
    $returnedObj | add-member NoteProperty -name “Database” -value $objItem.Name
    $returnedObj | add-member NoteProperty -name “Last Full (hrs ago)” -value $fullago
    $returnedObj | Add-Member NoteProperty -name “Last Inc (hrs ago)” -value $incago
    $returnedObj
    }

    Write-Host “Press any key to continue …”

    $x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”)

    1. Jeff

      Robert, log on to http://www.microsoftvirtualacademy.com/ and enrol/watch the course ‘Getting Started with PowerShell 3.0 Jump Start’. In that you will learn why double-clicking on a .PS1 file won’t work (and why this is actually a GOOD thing!)

      You’ll probably have a bit of a laugh here and there too.

      PS – It’s free!

  4. simon hawkes

    Hi Paul

    Great script. As I am just learning PowerShell I only get about 70% of the script but it works fine on our Exchange 2007 box so thanks for that. One thing I have been trying is to output the results to a file so the Exchange Team can check the databases are being backed up. I have tried using a pipe to Out-file followed by the path to the text file but I end up with a text file with no data in it  if you could possibly advise how to output to a file it would be very much appreciated

    Thanks in advance

    Simon

      1. Simon Hawkes

        Thanks Paul

        very funky 🙂

Leave a Reply