Using SQL Server 2008R2.
We are trying to audit user logins to see both how long user accounts have been inactive and to see how many licenses we will need coming up. I am currently pulling information from the following job:
Insert into [User_Login_Audit]
Select
login_name,
max (login_time) as last_login_time,
last_successful_logon,
(select @@servername) as server_instance,
getdate()
from sys.dm_exec_sessions
group by login_name, last_successful_logon;
I am then using linked servers to pull the information in via the following query:
Use B_DBA
GO
SELECT
[Login_name]
,max([last_login_time]) as Last_login_date
,[server_instance],
DATEDIFF(day, getdate(), max([last_login_time])) Days
FROM [B_DBA].[dbo].[User_Login_Audit]
where Login_name not in ('NT AUTHORITY\SYSTEM', 'sa','')
and last_successful_logon is not null
group by Login_name,server_instance
This is pulling the results of
Server Name | Server\instance Login_name | PROD\account_name Last_login_date | 2016-10-16 11:29:05.950 server_instance | Server\instance Days |-24
The problem I have with this is that apparently sys.dm_exec_sessions is not giving me current active users that are in Security now, but all users that have logged in, many of which have been removed from current access in security.
What I am needing is a way to pull Sql Users, Windows Users and Windows User Group logins that are currently in Database security. I need to know the last time the user logged in so I can calculate the number of days ie. 30,60,90.
What I am doing is giving me information, but not the most accurate information and is requiring a great deal of extra work. I would appreciate different options on this.