WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADSI
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Users Last Logon Time

 Keywords:  Last Login Logon Timestamp Time Date lastLogon LastLoginTimestamp

IMPORTANT NOTE: The lastLogon attribute is not replicated throughout the domain! You may need to use the property lastLogonTimestamp if available.

Read: http://www.microsoft.com/technet/scriptcenter/topics/win2003/lastlogon.mspx


Here is some sample code I put together that uses the ADSI extender to retrieve the users last logon date and time.

AddExtender("WWADS44I.DLL")
;
; samAccountName of user of interest
;
MyUser = "deanad" ;!!!! MODIFY TO FIT YOUR NEEDS
ServerDn=dsGetProperty("LDAP://rootDSE", "serverName")
ServerName=ItemExtract(1, ServerDn, ",")
ServerName=ItemExtract(2, ServerName, "=")
ServerDn=dsGetProperty("LDAP://rootDSE", "defaultNamingContext")
ServerPath="LDAP://%ServerName%/%ServerDN%"
UserPath=dsFindPath(ServerPath, "(&(objectCategory=person)(sAMAccountName=%MyUser%))")
;
; Check if user exists
;
If dsIsObject(UserPath )
   Message(UserPath, "User Exists")
Else   
	Message(UserPath, "User Does NOT exist.")
EndIf
;
;  User Account Last Login
;
result  = dsGetProperty(UserPath, "lastLogon")
Message("lastLogon", result)  
Exit


This sample gets the 64 bit value of the lastLogonTimestamp:

AddExtender("WWADS44I.DLL")
;
; samAccountName of user of interest
;
MyUser = "deanad"
ServerDn=dsGetProperty("LDAP://rootDSE", "serverName")
ServerName=ItemExtract(1, ServerDn, ",")
ServerName=ItemExtract(2, ServerName, "=")
ServerDn=dsGetProperty("LDAP://rootDSE", "defaultNamingContext")
ServerPath="LDAP://%ServerName%/%ServerDN%"
UserPath=dsFindPath(ServerPath, "(&(objectCategory=person)(sAMAccountName=%MyUser%))")
;
objUser = GetObject(UserPath)
objLastLogon = objUser.Get("lastLogonTimestamp")

intLastLogonTime = objLastLogon.HighPart * (2^32) + objLastLogon.LowPart 
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440

Message( "Last logon time: " , intLastLogonTime)

Exit

Article ID:   W17366
File Created: 2008:04:10:15:08:30
Last Updated: 2008:04:10:15:08:30