dsquery * -filter "(&(objectCategory=user)(objectClass=user))" -limit 0 -attr distinguishedName sAMAccountName lastLogon > \\192.168.1.1\LogonData\%COMPUTERNAME%_logontimes.txt
if object_id('[dbo].[fn_GetLastLogonDt]') is not null
begin
drop function [dbo].[fn_GetLastLogonDt]
end
go
create function [dbo].[fn_GetLastLogonDt](@LastLogonStamp bigint)
returns datetime
as
/************************************************************
*
* Author: Jim P.
* Last modified: 02/07/2013
* Purpose: Converts the last logon microsecond
* from Active Directory into a regular
* date value
*
*
*************************************************************/
begin
declare @LastLogin as bigint
declare @ReturnDt as datetime
-- If the timestamp is too large, or small you get an error.
-- so I'm putting in an error handling routine to give an
-- obviously false date to handle that.
if @LastLogonStamp < 47967800000000000 or
@LastLogonStamp > 2650468000000000000
begin
select @ReturnDt = case
when @LastLogonStamp > 2650000000000000000 then cast('9999-12-31 23:59:59:59' as datetime) --@ReturnDt =
when @LastLogonStamp < 47967800000000000 then cast('1753-01-01 00:00:00.000' as datetime) -- 1753-01-01 00:00:00.000
end
end
else
begin
set @LastLogin = @LastLogonStamp
set @LastLogin = @LastLogin / (60 * 10000000)
set @LastLogin = @LastLogin / 1440
set @LastLogin = @LastLogin - (datediff(dd, '1/1/1900', dateadd(yy, 299, '1/1/1900')))
select @ReturnDt = dateadd(day,@lastlogin,'1/1/1900')
end
return @ReturnDt
end
go
select [dbo].[fn_GetLastLogonDt](129956570270092500)
select [dbo].[fn_GetLastLogonDt](4000000000000000)
select [dbo].[fn_GetLastLogonDt](46000000000000000)
select [dbo].[fn_GetLastLogonDt](2750000000000000000)
select [dbo].[fn_GetLastLogonDt](1650468000000000000)
dsquery user -limit 0 | dsget user -dn -samid -ln -fn -display -mustchpwd -canchpwd -pwdneverexpires -disabled -acctexpires > \\192.168.1.1\LogonData\%COMPUTERNAME%_logonnames.txt
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
For those looking for additional information on the topic, including a TechNet article, please consider reading my Converting Active Directory Timestamps in Microsoft SQL Server article.