Taking dbatools Test-DbaLastBackup a little further

In a previous post I showed how easy it is to test your backups using Test-DbaLastBackup

Today I thought I would take it a little further and show you how PowerShell can be used to transmit or store this information in the manner you require

Test-DBALastBackup returns an object of information

SourceServer  : SQL2016N2
TestServer    : SQL2016N2
Database      : FadetoBlack
FileExists    : True
RestoreResult : Success
DbccResult    : Success
SizeMB        : 1243.26
BackupTaken   : 3/18/2017 12:36:07 PM
BackupFiles   : Z:\SQL2016N2\FadetoBlack\FULL_COPY_ONLY\SQL2016N2_FadetoBlack_FULL_COPY_ONLY_20170318_123607.bak

which shows the server, the database name, if the file exists, the restore result, the DBCC result, the size of the backup file, when the backup was taken and the path used

Text File

As it is an object we can make use of that in PowerShell. We can output the results to a file

01 - out file.PNG


Or maybe you need a CSV

02 - csv file.PNG


Maybe you want some json

06 - json results.PNG


Or an HTML page

03 - html.PNG


or perhaps you want a nice colour coded Excel sheet to show your manager or the auditors


It looks like this. Green is Good, Red is Bad, Grey is don’t care!


You might need to email the results, here I am using GMail as an example. With 2 factor authentication you need to use an app password in the credential

07 -email
You can of course attach any of the above files as an attachment using the -attachment parameter in Send-MailMessage


Of course, as good data professionals we probably want to put the data into a database where we can ensure that it is kept safe and secure

dbatools has a couple of commands to help with that too. We can use Out-DbaDataTable to create a datatable object and Write-DbaDatatable to write it to a database

Create a table

then add the data

and query it

08 - Database.PNG

Hopefully that has given you some ideas of how you can make use of this great command and also one of the benefits of PowerShell and the ability to use objects for different purposes

Happy Automating

NOTE – The major 1.0 release of dbatools due in the summer 2017 may have breaking changes which will stop the above code from working. There are also new commands coming which may replace this command. This blog post was written using dbatools version 0.8.942 You can check your version using

and update it using an Administrator PowerShell session with

You may find that you get no output from Update-Module as you have the latest version. If you have not installed the module from the PowerShell Gallery using

Then you can use





10 thoughts on “Taking dbatools Test-DbaLastBackup a little further

  1. Pingback: Using Pester with dbatools Test-DbaLastBackup | SQL DBA with A Beard

  2. Pingback: Test your Sqlserver backups on Linux with PowerShell and dbatools | SQL DBA with A Beard

  3. Pingback: Test the SQL Server database collation with PowerShell and dbatools | SQL DBA with A Beard

  4. Pingback: Getting SQLServers Last Known Good DBCC Checkdb with PowerShell and dbatools | SQL DBA with A Beard

  5. Pingback: Testing the Identity Column usage in SQL Server with PowerShell and dbatools | SQL DBA with A Beard

  6. I just went through all your examples. I am intrigued by the Test-DbaLastBackup. While we have a TSQL based solution that does this. I was thinking of switching it over. But I had a couple questions. How would you go about pointing to an existing Full/Diff/Log instead of taking a backup then restoring it? Some of our databases are large and a backup at runtime would not be feasible. Also, this process I would guess be better fit around your backup routines to make sure things are restoreable. Just looking for some guidance on expanding on this solution.

Please feel free to comment on this post. All comments are moderated first before appearing on the site