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 failed login attempts


This recipe lists failed login attempts in your SQL Server instance.

Getting ready

In order to get some entries in this recipe, you will need to simulate some failed login attempts in your SQL Server instance, if you don't already have any previous failed logins. One way to do this is to try logging in to SQL Server using SQL Server Management Studio and providing an incorrect username or password.

How to do it...

To check the failed login attempts, follow these steps:

  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
  3. Add the following script and run:

    #According to MSDN:
    #ReadErrorLog returns A StringCollection system object
    #value that contains an enumerated list of errors
    ...