I'm trying to get some data from the Active Directory in SQL. I use the code:

SELECT * FROM OpenQuery 
'SELECT  objectGUID, objectSID, employeeID, lastLogon, mobile, telephoneNumber, mail,  department, givenName, sn, displayname,  sAmAccountName
FROM ''LDAP://DC=mycompany''    
where  objectClass = ''user''
and sAmAccountName = ''testuser'''  

My problem is that the value i receive for lastLogon isn't refreshing. It is possible that the other values aren't also refreshing, however i can't say this for sure, because in the other data there is no change, but the lastLogon value should be updated when i lock my computer and log in.

Does anyone have any idee why is this, and how can i solve this?


migrated from stackoverflow.com Feb 1 '12 at 14:07

This question came from our site for professional and enthusiast programmers.


The lastLogon attribute reflects the last interactive logon, not the last network-based logon

More info found here

This is some excellent information from this site

Another twist: Active Directory does NOT replicate the LastLogon attribute across domain controllers. So in order to get an accurate value, you need to obtain the LastLogon value for the same user from all your domain controllers and accept the value that is highest.

  • if i make a query from a C++ application then it is updated. Why is that? – kampi Feb 1 '12 at 13:37
  • @kampi: IIRC, An ADSI linked server hits the same DC, not the domain itself. – gbn Feb 1 '12 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.