Listing failed login attempts
This recipe lists failed login attempts in your SQL Server instance.
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
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 = "KERRIGAN" $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. $server.ReadErrorLog() | Where-Object ProcessInfo -Like "*Logon*" | Where-Object Text -Like "*Login failed*" | Format-List
How it works...
One way to
get failed login attempts is by using the method ReadErrorLog
of the SMO Server object and filtering by ProcessInfo
and Text
properties...