Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Checking last backup date


In this recipe, we will check when databases have been last backed up.

Getting ready

One way to check when a database was last backed up is through SQL Server Management Studio. Open SSMS and connect to your instance. For example, if you want to see when AdventureWorks2014 was last backed up, you can right-click on this database and select Properties. In the General page, you should see a section for Backup, which should show you when a database and log were last backed up.

How to do it...

The steps to check the last backup dates are as follows:

  1. Open PowerShell ISE as administrator.

  2. Import the SQLPS module as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run:

    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
    
    
    $server.Databases |
    Select-Object Name, RecoveryModel,
    LastBackupDate,
    LastDifferentialBackupDate,
    LastLogBackupDate |
    Format-Table...