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

Listing SQL Server log errors


In this recipe, we will list SQL Server log errors past a specific date.

Getting ready

Go to SQL Server Management Studio and log in to your instance. Once logged in, expand SQL Server Agent | Error Logs | Current.

For our recipe, we will check the logs that have failed entries past March 1, 2015. Feel free to change these parameters when you do this recipe.

How to do it...

Let's check how we can list SQL Server errors using PowerShell:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new SMO Server Object as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName

    Add the following script and run:

    #According to MSDN:
    #ReadErrorLog: returns A StringCollection system object
    #value that contains an enumerated list of errors from
    #the SQL Server error log.
    [datetime...