is there a way to find out logins that have not been logged into the SQL Server or accessed the databases in the past 90 days ?
Sql-server – SQL unused logins
maintenancesql serversql-server-2012
Related Question
- Sql-server – Delete SQL Server logins on replicated server
- Sql-server – SQL Server Reporting Services: How to check who accessed certain report
- Sql-server – SQL Script to Drop old AD logins, Inactive SQL Logins and all related Users
- Sql-server – SQL logins access
- Sql-server – How to identify database users and their owned schemas before dropping server logins
- Sql-server – SQL Server – Remove all logins that they are asossiated with orphan users
- Sql-server – How to find unused logins in SQL Server
Best Answer
You can capture this information with an Extended Events session:
Then you can stream the events captured in the session and process them with a powershell script.
First of all, you will need a couple of tables to store the results. In this case you can use a staging table to store the events temporarily and a target table to store the events in a summarized form. You are not interested in the individual events, you only need to capture when the last successful logon was recorded. In case this makes sense to you, you can group events by some meaningful attributes:
Create these tables in
master
:Then you will need a stored procedure to consolidate the data from the staging to the target table:
The interesting part is the powershell script that reads the events from the session stream. Save the script as
c:\capture_logon_events.ps1
:The script can be run by a SQLAgent powershell step in a job. Create a job, set it to run when SQLAgent starts, add a powershell step with this code:
Then you need another job to consolidate the events using the stored procedure: set it to run every 5 minutes and add a T-SQL step with the call to the consolidate stored procedure.
And you're done. Looks complicated, but it's not.