Retrieving password policy settings for SQL login accounts

Today, I wrote the following query for our internal audit report for SAS70. This query provides all the necessary details about SQL Logins policy settings.

This query is using LOGINPROPERTY function to retrieve the sql login policy settings information:

USE [master]
GO

DECLARE @PwdExpirationAge [int]

SET @PwdExpirationAge = 28

SELECT [name] AS [SQL_User]
	,LOGINPROPERTY([name], 'PasswordLastSetTime') AS [PasswordLastResetDT]
	,@PwdExpirationAge - DATEDIFF(DAY, CONVERT([datetime], LOGINPROPERTY([name], 'PasswordLastSetTime')), GETDATE()) AS [DaysUntilExpiration]
	,LOGINPROPERTY([name], 'BadPasswordCount') AS [BadPasswordCount]
	,LOGINPROPERTY([name], 'BadPasswordTime') AS [BadPasswordDT]
	,LOGINPROPERTY([name], 'HistoryLength') AS [HistoryLength]
	,LOGINPROPERTY([name], 'IsExpired') AS [IsExpired]
	,LOGINPROPERTY([name], 'IsLocked') AS [IsLocked]
	,LOGINPROPERTY([name], 'IsMustChange') AS [IsMustChange]
	,LOGINPROPERTY([name], 'LockoutTime') AS [LockoutTime]
FROM [sys].[sql_logins]
GROUP BY [name];
GO

I hope you will find this query useful.

About these ads

One thought on “Retrieving password policy settings for SQL login accounts

  1. Great script, works like a charm on 2008 R2 as well.
    I changed the SELECT [name] AS [sa] to SELECT [name] AS [Username] instead though, for my application that made more sense.

    Like this

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s