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.

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

Leave a comment