{"id":14061,"date":"2011-05-02T00:01:00","date_gmt":"2011-05-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/05\/02\/use-powershell-to-report-sql-server-backup-status\/"},"modified":"2011-05-02T00:01:00","modified_gmt":"2011-05-02T00:01:00","slug":"use-powershell-to-report-sql-server-backup-status","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-report-sql-server-backup-status\/","title":{"rendered":"Use PowerShell to Report SQL Server Backup Status"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to use Windows PowerShell to automatically email a report on SQL Server backup status.<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/> Hey, Scripting Guy! <\/p>\n<p>&mdash;CN<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello CN, Microsoft Scripting Guy, Ed Wilson, here. The time is near for <a target=\"_blank\" href=\"http:\/\/sqlrally.com\/\">SQLRally<\/a> in Orlando. I will be making a presentation about Windows PowerShell and I wanted to support the rally. I thought the best way to do that would be to have a week of SQL blog posts and asked my friend, Microsoft SQL MVP Aaron Nelson (aka SQLVariant) if he had anything to share. Boy, did he come through! He rounded up four guest bloggers, and I rounded out the week with a newcomer to our ever growing circle of scripters. First up is Nicholas Cain. <\/p>\n<blockquote>\n<p>Nicholas Cain is a senior SQL Server DBA with over 10 years experience. He has a great deal of experience working in large enterprises with databases ranging in size from a few MB to multi-TB. He holds MCITP certifications in both development and database administration with SQL Server 2008. A fan and speaker at <a target=\"_blank\" href=\"http:\/\/www.sqlsaturday.com\/\">SQLSaturday<\/a> events, Nicholas really enjoys the community aspects of SQL Server, and he can be found on Twitter as @AnonyTheMouse and at his blog <a target=\"_blank\" href=\"http:\/\/englishtosql.com\/\">English to SQL<\/a>. <\/p>\n<\/blockquote>\n<h3>Checking SQL Server backups<\/h3>\n<p>A couple of years ago, I moved from New Jersey to Washington State, and I was fortunate enough to work at a location that was not based in one of the major metropolitan areas. I recently changed jobs. One of the saddest parts of the change for me was not being able to look out the office window to get a clear view of the Cascade Mountains.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2783.HSG-5-2-11-01_028CBCEC.jpg\"><img decoding=\"async\" height=\"452\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3377.HSG-5-2-11-01_thumb_72BD021A.jpg\" alt=\"Photo of mountain view\" border=\"0\" title=\"Photo of mountain view\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>A view like this does not cost anything&mdash;although I wish I had a dollar for every time I have been asked over the last 10 years when a database was last backed up. I would not be a rich man; however, my income would be supplemented nicely. <\/p>\n<p>Backups are vital for any business, and databases frequently keep the business running. Imagine your database server goes down, and on that server is your customer list and all invoice information. Without a backup, you cannot recover the database and your business goes under. You need to know that your databases are being backed up. You also need to be able to quickly check and confirm that every database on every server is being backed up. <\/p>\n<p>It is a challenge for any SQL Server DBA to have this information at hand, and if you are only a part-time or accidental DBA, that difficulty can easily be exponentially worse. For a single server, this is relatively simple. But if you are responsible for ten, twenty, fifty, or more SQL instances, you&rsquo;ll find yourself very quickly overwhelmed. (A SQL instance is a defined individual installation of the SQL Server services. You can have multiple instances on a physical server&mdash;one can be a default instance, and the others have to be named. See <a target=\"_blank\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms143694.aspx\">Working with Multiple Versions and Instances of SQL Server<\/a> on MSDN for more details).<\/p>\n<h3>So how can I check my backups?<\/h3>\n<p>To the rescue come Windows PowerShell and the SQL snap-ins. So much information about SQL instances, databases, tables, procedures, etc., is quickly and easily exposed through these snap-ins. They really make the life of the SQL Server DBA such a more pleasant experience.<\/p>\n<p>You are going to want to do this on a machine with the SQL snap-ins installed. The quickest and easiest way to get the snap-ins (if you do not already have them) is down download a copy of <a target=\"_blank\" href=\"http:\/\/www.microsoft.com\/downloads\/en\/details.aspx?FamilyID=56ad557c-03e6-4369-9c1d-e81b33d8026b\">SQL Server Management Studio<\/a> from Microsoft. If you are running this on a machine with SQL 2008 or later installed, you should be good to go.<\/p>\n<p>So open a Windows PowerShell editor (it doesn&rsquo;t matter which one, choose your favorite) and load the snap-ins by using the following two commands.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Add-pssnapin SqlServerCmdletSnapin100 <br \/>Add-pssnapin SqlServerProviderSnapin100<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Now we are good to go. <\/p>\n<p>In the following examples, I&rsquo;ll be querying a localized installation of SQL Server. Change out &ldquo;localhost\\DEFAULT&rdquo; for your SQL Server. Use DEFAULT if it is not a named instance (that is, server STAGE1 would be STAGE1\\DEFAULT). For example, a named instance on a server with the name of STAGE2 and an instance name of Instance1 would be STAGE2\\Instance1.<\/p>\n<p>Now let us quickly grab a list of databases on the SQL instance and show their last backup date. The following command accomplishes this task.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">dir SQLSERVER:\\SQL\\localhost\\DEFAULT\\Databases | Select Name, LastBackupDate | Out-GridView<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>The output display is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3463.hsg-5-2-11-2_21530AFD.jpg\"><img decoding=\"async\" height=\"334\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8233.hsg-5-2-11-2_thumb_4BDEC60D.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>Hey cool, that was easy, let us go home!<\/p>\n<p>Not so fast. <\/p>\n<p>There is a whole host of information available that can be exposed that we haven&rsquo;t captured, not to mention that for some reason the Master, Model, and MSDB databases aren&rsquo;t being shown.<\/p>\n<p>System databases are hidden and we cannot examine their properties without using the <i>Force<\/i> parameter. So let us try that again with that parameter included:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">dir -force SQLSERVER:\\SQL\\localhost\\DEFAULT\\Databases | Select Name, LastBackupDate | Out-GridView<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7288.hsg-5-2-11-3_2142E530.jpg\"><img decoding=\"async\" height=\"470\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2870.hsg-5-2-11-3_thumb_68D4BE57.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>Now we see those system databases. <\/p>\n<h3>Getting more backup information<\/h3>\n<p>So we have the basics. This is a great start; however, we are actually only getting the last time a full backup of the database was performed. If a differential backup had been taken, we would not have known.<\/p>\n<p>Even worse, there is a problem with the results. Run the script and note the retrieved results. Now take a backup of one of your databases and run the script again. You will see that the LastBackupDate value has not changed. This is because the data gets cached. To be sure that we always get the most current information we have to refresh the data.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">dir -force SQLSERVER:\\SQL\\localhost\\DEFAULT\\Databases | where-object {$_.Name; $_.Refresh()} | Select Name, LastBackupDate, LastDifferentialBackupDate | Out-GridView<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2474.hsg-5-2-11-4_414DCC20.jpg\"><img decoding=\"async\" height=\"366\" width=\"404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7673.hsg-5-2-11-4_thumb_1DD127BB.jpg\" alt=\"Image of command output\" border=\"0\" title=\"Image of command output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<h3>Making improvements<\/h3>\n<p>So now we have the date for the last time the database was backed up, be it a full or differential backup. This is still not particularly readable though and it shows all the databases on the SQL instance. So how about a couple of improvement ideas:<\/p>\n<ul>\n<li>Turn this into an easily executed function that we could put in our profile ready to call.<\/li>\n<li>Ignore tempdb because it doesn&rsquo;t get backed up. (It gets re-created whenever SQL Server is started, so there&rsquo;s no point.)<\/li>\n<li>Tell us whether the last backup was a full or differential as a part of the results.<\/li>\n<li>Tell us how long ago the last backup was completed so we don&rsquo;t have to figure it out for ourselves.<\/li>\n<li>Provide us the option to grab information for a single database rather than all of them.<\/li>\n<li>Give us a clear warning if a database has never been backed up.<\/li>\n<\/ul>\n<p>These ideas lead to the following function. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Function Get-DBBackupInfo ($SQLInstance, $DBName)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">if ($SQLInstance.Contains(&#8220;`\\&#8221;))<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{ $location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\Databases&#8221; }<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{ $location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\DEFAULT\\Databases&#8221; }<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DisplayResults =<span>&nbsp;&nbsp; <\/span>@{Label=&#8221;DB Name&#8221;;Expression={$_.Name};width=30},<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">@{Label=&#8221;Last Full&#8221;;Expression={IF ($_.LastBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {&#8220;NA&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {$_.LastBackupDate.ToString(&#8220;yyyy\/MM\/dd HH:mm:ss&#8221;)}};width=25},<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">@{Label=&#8221;Last Differential&#8221;;Expression={IF ($_.LastDifferentialBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {&#8220;NA&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {$_.LastDifferentialBackupDate.ToString(&#8220;yyyy\/MM\/dd HH:mm:ss&#8221;)}};width=25}, <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">@{Label=&#8221;Most Recent Backup Type&#8221;;Expression={IF ($_.LastBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {&#8220;NA&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSEIF ($_.LastBackupDate -gt $_.LastDifferentialBackupDate) {&#8220;FULL&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {&#8220;DIFF&#8221;}};width=25},<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">@{Label=&#8221;Days Since Last Backup&#8221;;Expression={IF ($_.LastBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {&#8220;Never Backed Up!&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSEIF ($_.LastDifferentialBackupDate -gt $_.LastBackupDate) {((<b>Get-Date<\/b>) &#8211; $_.LastDifferentialBackupDate).Days} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {((<b>Get-Date<\/b>) &#8211; $_.LastBackupDate).Days}};width=25} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">if ($DBName)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>dir<\/b> <i>-force<\/i> $location | <b>where-object<\/b> {$_.Name -eq $DBName; $_.Refresh()} | <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>format-table<\/b> $DisplayResults <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else<\/span><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>dir<\/b> <i>-force<\/i> $location | <b>where-object<\/b> {$_.Name -ne &#8220;tempdb&#8221;; $_.Refresh()} | <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>format-table<\/b><span>&nbsp; <\/span>$DisplayResults <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">}<\/span><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Now to return good, relevant information, we simply call this function along with the SQL instance name as shown here.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Get-DBBackupInfo localhost<\/span><\/span><\/span><span style=\"font-family:\"><\/span><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3060.hsg-5-2-11-5_7E5ED127.jpg\"><img decoding=\"async\" height=\"185\" width=\"704\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2870.hsg-5-2-11-5_thumb_5024D56D.jpg\" alt=\"Image of command and output\" border=\"0\" title=\"Image of command and output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>The returned results clearly show us when databases were last backed up and the type of backup&mdash;and databases that have not been backed up really stand out.<\/p>\n<p>It is still a lot of information to digest if we are only interested in a single database. The way the function was written, we also have the option to provide a database name and only return results for that. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Get-DBBackupInfo localhost adventureworks<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1881.hsg-5-2-11-6_4CC33DC5.jpg\"><img decoding=\"async\" height=\"42\" width=\"704\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7563.hsg-5-2-11-6_thumb_22938FDD.jpg\" alt=\"Image of command and output\" border=\"0\" title=\"Image of command and output\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>Armed with this, you can very quickly retrieve backup information for a SQL instance and database, so when that user comes a knocking, you can provide quick and accurate results.<\/p>\n<h3>Being more proactive<\/h3>\n<p>Now that you can quickly get folks out of your hair, let&rsquo;s take it to the next step. Wouldn&rsquo;t it be great if you could get that sort of information for all of your SQL Servers, have all that information stored in a single location where you could query it as needed, and send you an email in the event that you have databases that are not being backed up?<\/p>\n<p>This can be done relatively simply thanks to a variation of the previous script by using <b>Invoke-SqlCmd<\/b> and <b>Send-MailMessage<\/b>.<\/p>\n<p>Those of you who experienced with SQL Server will say, &ldquo;I could just query MSDB and get the information I need.&rdquo; <\/p>\n<p>Yes, you absolutely could; however, MSDB is not going to tell you what databases have not been backed up, and depending on your maintenance and indexing strategies, it could take a significant amount of time to query that data. (For cleansing tips on MSDB data, take a look at <a target=\"_blank\" href=\"http:\/\/www.englishtosql.com\/english-to-sql-blog\/2011\/1\/25\/keeping-msdb-clean.html\">Keeping MSDB Clean<\/a>). Pulling all of that data from MSDB into a central location is certainly viable, but it&rsquo;s not as quick or as simple to maintain as using Windows PowerShell and the SQL snap-ins.<\/p>\n<h3>SQL setup<\/h3>\n<p>We are going to use SQL Server to hold all of our data, so we need to create a database and a couple of tables. The <b>Transact SQL (T-SQL)<\/b> commands to accomplish this are shown here. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">CREATE<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">DATABASE<\/span><\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000;font-size: 10pt\"> SQLInfo<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">GO<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">USE<\/span><\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000;font-size: 10pt\"> SQLInfo<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">GO<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">CREATE<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">TABLE<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> dbo<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">.<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">DatabaseBackups<\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">(<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">SERVERNAME <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">varchar<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">(<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">50<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">)<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">DatabaseName <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">varchar<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">(<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">128<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">)<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">LastFullBackupDate <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">datetime<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">LastDifferentialBackupDate <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">datetime<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">LastAbsoluteBackupDate <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">datetime<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">LastAbsoluteBackupType <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">char<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">(<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">4<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">)<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">NOT<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL,<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">BackupDelta <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">int<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">NOT<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">NULL<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #808080\"><span style=\"font-size: 10pt\">)<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">ON<\/span><\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000;font-size: 10pt\"> [PRIMARY]<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">GO<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #0000ff;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">CREATE<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">TABLE<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> [dbo]<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">.<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">[SQLInstances]<\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">(<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span style=\"font-size: 10pt\">InstanceName <\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">varchar<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">(<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\">128<\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #808080\"><span style=\"font-size: 10pt\">)<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">ON<\/span><\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000;font-size: 10pt\"> [PRIMARY]<\/span><\/span><\/span><span style=\"font-family:;color:\"><\/span><\/p>\n<\/blockquote>\n<p>The SQLInstances table is going to hold a list of our SQL Server instances. We&rsquo;ll iterate through this list and store backup information for all of the databases on each in the DatabaseBackups table.<\/p>\n<p>To get started, add a single record to the table. This keeps the result set low initially while we ensure that everything is running smoothly.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">INSERT<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">INTO<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> SQLInstances <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">VALUES <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">(<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #ff0000\">&#8216;localhost&#8217;<\/span><\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080;font-size: 10pt\">)<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Now we are ready for a new function:<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Function Get-DBBackupToDatabase ($SQLInstance)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Check whether or not a named instance and set the location accordingly<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">if ($SQLInstance -ilike &#8220;*\\*&#8221;) {$location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\Databases&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else {$location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\DEFAULT\\Databases&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DBStats = <b>dir<\/b> <i>-force<\/i> $location | <b>where-object<\/b> {$_.Name -ne &#8220;tempdb&#8221;; $_.Refresh()}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">foreach ($DB in $DBStats)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DBName = $DB.Name<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$LastFull = $DB.LastBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span>IF ($DB.LastDifferentialBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastDiff = $NULL} ELSE {$LastDiff = $DB.LastDifferentialBackupDate}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#The last absolute backup will be the newer of the last full or last differential, we can also set the type using this<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastDiff -gt $LastFull) {$LastAbsolute = $LastDiff; $LastType = &#8220;DIFF&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSEIF ($LastFull -eq &#8220;01\/01\/0001 00:00:00&#8221;){$LastAbsolute = $LastFull; $LastType = &#8220;NONE&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {$LastAbsolute = $LastFull; $LastType = &#8220;FULL&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Quick calculation gives us the number of days since the last backup<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DaysSince = ((<b>Get-Date<\/b>) &#8211; $LastAbsolute).Days<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Because SQL cannot store the default date of 01\/01\/0001 we set it to null, which will store in sql as 1900-01-01<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastFull -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastFull = $NULL}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastAbsolute -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastAbsolute = $NULL}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$InsertResults = @&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">INSERT INTO dbo.DatabaseBackups (ServerName, DatabaseName, LastFullBackupDate, LastDifferentialBackupDate, LastAbsoluteBackupDate, LastAbsoluteBackupType, BackupDelta)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">VALUES (&#8216;$SQLInstance&#8217;, &#8216;$DBName&#8217;, &#8216;$LastFull&#8217;, &#8216;$LastDiff&#8217;, &#8216;$LastAbsolute&#8217;, &#8216;$LastType&#8217;, &#8216;$DaysSince&#8217;)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: 10pt\">&#8220;@<\/span><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> $InsertResults<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>This function will take a SQL instance, grab all the backup information, do a couple of quick calculations on it and then write that information into the dbo.DatabaseBackups table.<\/p>\n<p>Note that the <b>Invoke-SqlCmd<\/b> here is using <b>@params<\/b> for the <i>ServerName<\/i> and <i>Database<\/i> parameters. This allows us to only specify that information when calling the function and not have to put it in the script multiple times (which really helps when it comes to maintenance).<\/p>\n<p>To call this for every SQL instance listed in the SQLInstances table, we grab a record set and then iterate through, passing each record to the function as follows.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Connection information for the database server where the SQLInstances and DatabaseBackups tables reside<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$params = @{&#8216;server&#8217;=&#8217;localhost&#8217;;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">&#8216;Database&#8217;=&#8217;SQLInfo&#8217;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Grab our list of servers, iterate through them and call the function which rights to the database<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$Srv = <b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> &#8220;TRUNCATE TABLE dbo.DatabaseBackups;SELECT InstanceName from SQLInstances&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">foreach ($Instance in $srv)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Get-DBBackupToDatabase $Instance.InstanceName<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">}<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>All our backup information will now be loaded into the DatabaseBackups table. We can run a quick SQL query to check this.<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"font-family:;color:\"><span style=\"color: #0000ff\"><span style=\"font-size: 10pt\">SELECT<\/span><\/span><\/span><span style=\"font-size: 10pt\"><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">*<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> <\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #0000ff\">FROM<\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000\"> dbo<\/span><\/span><span style=\"font-family:;color:\"><span style=\"color: #808080\">.<\/span><\/span><\/span><span style=\"font-family:\"><span style=\"color: #000000;font-size: 10pt\">DatabaseBackups<\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2474.hsg-5-2-11-7_6D3A57AA.jpg\"><img decoding=\"async\" height=\"226\" width=\"704\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3858.hsg-5-2-11-7_thumb_5070BCC8.jpg\" alt=\"Image of backup table\" border=\"0\" title=\"Image of backup table\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<p>If you have 10, 20, or 100 SQL instances, being able to quickly load all backup information into a single table and query it to find that information is immensely useful. To make it more useful though, we should send that email notification.<\/p>\n<h3>Emailing backup information<\/h3>\n<p>Once more, <b>Invoke-SqlCmd<\/b> comes to the rescue. We grab the information that we want from SQL, pretty it up a little by converting the results to HTML, apply some style sheet information, and then send the email (you need to change the requisite to\/from\/smtp information so that the email will send).<\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Stylesheet info used for returning good looking results<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = &#8216;&lt;style&gt;&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;BODY{background-color:peachpuff;}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:thistle;font-size:16}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style+ &#8216;TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:palegoldenrod;font-size:14}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;&lt;\/style&gt;&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#So we can get some information back we pull a list of databases not backed up in the previous day<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#And send them our email recipient<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$FailedBackups = <b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> &#8220;Select ServerName, <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">DatabaseName<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">, case LastAbsoluteBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">when &#8216;1900-01-01 00:00:00&#8217; then &#8216;Never Backed Up&#8217;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else convert(varchar(20), LastAbsoluteBackupDate, 120)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">end as LastBackup<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">, case LastAbsoluteBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>when &#8216;1900-01-01 00:00:00&#8217; then &#8216;Never Backed Up&#8217;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else convert(varchar,BackupDelta)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">end as BackupDelta<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">from dbo.DatabaseBackups where BackupDelta &gt; 0&#8243; | <b>convertTo-Html<\/b> <i>-Head<\/i> $style <i>-Property<\/i> ServerName, DatabaseName, LastBackup, BackupDelta | <b>out-string<\/b>;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Don&#8217;t forget to set your smtpserver information<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 8pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><b><span style=\"font-family:;color:\"><span style=\"font-size: 10pt\">Send-MailMessage<\/span><\/span><\/b><span style=\"font-family:;color:\"><span style=\"font-size: 10pt\"> <i>-To<\/i> &#8220;me@me.com&#8221; <i>-Subject<\/i> &#8220;Databases Not Backed Up&#8221; <i>&ndash;From<\/i> &#8220;me@me.com&#8221; <i>-SmtpServer<\/i> &#8220;mysmtpserver&#8221; <i>-Body<\/i> $FailedBackups <\/span><i><span style=\"font-size: 10pt\">&ndash;BodyAsHtml<\/span><\/i><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>Your email should look something like this (assuming you have databases that have not been backed up in the last day).<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4431.hsg-5-2-11-8_7F06C5AA.jpg\"><img decoding=\"async\" height=\"506\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4530.hsg-5-2-11-8_thumb_097773FE.jpg\" alt=\"Image of email\" border=\"0\" title=\"Image of email\" style=\"border-bottom: 0px;border-left: 0px;padding-left: 0px;padding-right: 0px;border-top: 0px;border-right: 0px;padding-top: 0px\" \/><\/a><\/p>\n<h3>Putting it together<\/h3>\n<p>We now have a script that will do everything for us: grab the data, store it, and email us when there are backup issues. <\/p>\n<blockquote>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">Function Get-DBBackupToDatabase ($SQLInstance)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Check whether or not a named instance and set the location accordingly<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">if ($SQLInstance -ilike &#8220;*\\*&#8221;) {$location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\Databases&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else {$location = &#8220;SQLSERVER:\\SQL\\$SQLInstance\\DEFAULT\\Databases&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;<\/span><\/span><span><span style=\"font-size: 10pt\">&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DBStats = <b>dir<\/b> <i>-force<\/i> $location | <b>where-object<\/b> {$_.Name -ne &#8220;tempdb&#8221;; $_.Refresh()}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">foreach ($DB in $DBStats)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">{<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DBName = $DB.Name<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$LastFull = $DB.LastBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($DB.LastDifferentialBackupDate -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastDiff = $NULL} ELSE {$LastDiff = $DB.LastDifferentialBackupDate}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#The last absolute backup will be the newer of the last full or last differential, we can also set the type using this<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastDiff -gt $LastFull) {$LastAbsolute = $LastDiff; $LastType = &#8220;DIFF&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSEIF ($LastFull -eq &#8220;01\/01\/0001 00:00:00&#8221;){$LastAbsolute = $LastFull; $LastType = &#8220;NONE&#8221;} <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">ELSE {$LastAbsolute = $LastFull; $LastType = &#8220;FULL&#8221;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Quick calculation gives us the number of days since the last backup<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">$DaysSince = ((<b>Get-Date<\/b>) &#8211; $LastAbsolute).Days<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">#Because SQL cannot store the default date of 01\/01\/0001 we set it to null, which will store in sql as 1900-01-01<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastFull -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastFull = $NULL}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">IF ($LastAbsolute -eq &#8220;01\/01\/0001 00:00:00&#8221;) {$LastAbsolute = $NULL}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$InsertResults = @&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">INSERT INTO dbo.DatabaseBackups (ServerName, DatabaseName, LastFullBackupDate, LastDifferentialBackupDate, LastAbsoluteBackupDate, LastAbsoluteBackupType, BackupDelta)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">VALUES (&#8216;$SQLInstance&#8217;, &#8216;$DBName&#8217;, &#8216;$LastFull&#8217;, &#8216;$LastDiff&#8217;, &#8216;$LastAbsolute&#8217;, &#8216;$LastType&#8217;, &#8216;$DaysSince&#8217;)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span style=\"font-size: 10pt\">&#8220;@<\/span><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\"><b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> $InsertResults<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Connection information for the database server where the SQLInstances and DatabaseBackups tables reside<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$params = @{&#8216;server&#8217;=&#8217;localhost&#8217;;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">&#8216;Database&#8217;=&#8217;SQLInfo&#8217;}<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Grab our list of servers, iterate through them and call the function which rights to the database<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$Srv = <b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> &#8220;TRUNCATE TABLE dbo.DatabaseBackups;SELECT InstanceName from SQLInstances&#8221;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">foreach ($Instance in $srv)<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">{<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">Get-DBBackupToDatabase $Instance.InstanceName<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">}<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Stylesheet info used for returning good looking results<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = &#8216;&lt;style&gt;&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;BODY{background-color:peachpuff;}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color:thistle;font-size:16}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style+ &#8216;TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:palegoldenrod;font-size:14}&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$style = $style + &#8216;&lt;\/style&gt;&#8217;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\"><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#So we can get some information back we pull a list of databases not backed up in the previous day<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#And send them our email recipient<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">$FailedBackups = <b>invoke-sqlcmd<\/b> @params <i>-Query<\/i> &#8220;Select ServerName, <\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">DatabaseName<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">, case LastAbsoluteBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">when &#8216;1900-01-01 00:00:00&#8217; then &#8216;Never Backed Up&#8217;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else convert(varchar(20), LastAbsoluteBackupDate, 120)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">end as LastBackup<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">, case LastAbsoluteBackupDate<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">when &#8216;1900-01-01 00:00:00&#8217; then &#8216;Never Backed Up&#8217;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">else convert(varchar,BackupDelta)<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">end as BackupDelta<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><span><span style=\"font-size: 10pt\">&nbsp;&nbsp;&nbsp; <\/span><\/span><span style=\"font-size: 10pt\">from dbo.DatabaseBackups where BackupDelta &gt; 0&#8243; | <b>convertTo-Html<\/b> <i>-Head<\/i> $style <i>-Property<\/i> ServerName, DatabaseName, LastBackup, BackupDelta | <b>out-string<\/b>;<\/span><\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">&nbsp;<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family:;color:\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000;font-size: 10pt\">#Don&#8217;t forget to set your smtpserver information<\/span><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: 13.25pt;list-style-type: disc;margin: 0in 0in 0pt\"><span style=\"font-family: Lucida Sans Typewriter\"><span style=\"color: #000000\"><b><span style=\"font-family:;color:\"><span style=\"font-size: 10pt\">Send-MailMessage<\/span><\/span><\/b><span style=\"font-family:;color:\"><span style=\"font-size: 10pt\"> <i>-To<\/i> &#8220;me@me.com&#8221; <i>-Subject<\/i> &#8220;Databases Not Backed Up&#8221; <i>&ndash;From<\/i> &#8220;me@me.com&#8221; <i>-SmtpServer<\/i> &#8220;mysmtpserver&#8221; <i>-Body<\/i> $FailedBackups <\/span><i><span style=\"font-size: 10pt\">-BodyAsHtml<\/span><\/i><\/span><\/span><\/span><\/p>\n<\/blockquote>\n<p>You can save this script and execute it manually whenever you need to. However, to get the most out of it, I would recommend scheduling it daily by using SQL Server Agent. Doing this will give you early notification of issues and allow you to act quickly to get those databases backed up.<\/p>\n<p>CN, that is all there is to using Windows PowerShell to query for SQL Server database backups. SQL week will continue tomorrow when we will welcome another guest blogger. <\/p>\n<p>Thank you, Nicholas, for writing this blog and sharing your experience with our readers. <\/p>\n<p>I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell to automatically email a report on SQL Server backup status. Hey, Scripting Guy! &mdash;CN Hello CN, Microsoft Scripting Guy, Ed Wilson, here. The time is near for SQLRally in Orlando. I will be making a presentation about Windows PowerShell and I wanted to support the rally. I thought [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[146,56,255,3,176,45],"class_list":["post-14061","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-databases","tag-guest-blogger","tag-nicholas-cain","tag-scripting-guy","tag-sql-server","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell to automatically email a report on SQL Server backup status. Hey, Scripting Guy! &mdash;CN Hello CN, Microsoft Scripting Guy, Ed Wilson, here. The time is near for SQLRally in Orlando. I will be making a presentation about Windows PowerShell and I wanted to support the rally. I thought [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=14061"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/14061\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=14061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=14061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=14061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}