Thursday, September 07, 2006

AD last login time

Nice script to find a user's last logon time from AD. It even converts the time result to human readable format.

' This VBScript code prints the last logon timestamp for a user.
' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' Publisher: O'Reilly and Associates
' ISBN: 0-596-00466-4
' Book web site: http://rallenhome.com/books/adcookbook/code.html
' ---------------------------------------------------------------
' ------ SCRIPT CONFIGURATION ------
'strUserDN = "" ' e.g. cn=rallen,ou=Sales,dc=rallencorp,dc=com
strUserDN = "cn=Web Maxtor,ou=MyOffice,ou=MyOrganization,dc=MyDomain,dc=com"
' ------ END CONFIGURATION ---------
set objUser = GetObject("LDAP://" & strUserDN)
set objLogon = objUser.Get("lastLogonTimestamp")
intLogonTime = objLogon.HighPart * (2^32) + objLogon.LowPart
intLogonTime = intLogonTime / (60 * 10000000)
intLogonTime = intLogonTime / 1440
WScript.Echo "Approx last logon timestamp: " & intLogonTime + #1/1/1601#

No comments:

Post a Comment