By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

Retrieve the the computer a user last logged on too

maxamis4
Expert 100+
P: 295
Hello folks,

Here is the backgroup. I am creating an agent that can find a user in LDAP and return the last logon date. Now i am not sure if with active directory you can user the SAMAccountName to retrieve the last computer the users logged on to. But i am looking for a place where i can learn this information. I was hoping someone out there could point me in the right direction.

Below is my code which i have been working on. It can retrieve the samaccountname information perfectly. I just can't get the lastlogon to work nor can i find how to bridge the name with a computer on LDAP . Any help would be great

Expand|Select|Wrap|Line Numbers
  1. Function Get_User_Name_AD()
  2. Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
  3.  
  4. Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strLastLogin
  5.  
  6.  
  7.  
  8. ' Setup ADO objects.
  9.  
  10. Set adoCommand = CreateObject("ADODB.Command")
  11. Set adoConnection = CreateObject("ADODB.Connection")
  12. adoConnection.Provider = "ADsDSOObject"
  13. adoConnection.Open "Active Directory Provider"
  14. adoCommand.ActiveConnection = adoConnection
  15.  
  16.  
  17.  
  18. ' Search entire Active Directory domain.
  19.  
  20. Set objRootDSE = GetObject("LDAP://RootDSE")
  21.  
  22. strDNSDomain = objRootDSE.Get("defaultNamingContext")
  23. strBase = "<LDAP://" & strDNSDomain & ">"
  24.  
  25.  
  26. ' Filter on user objects.
  27. 'KNOWN OBJECTS
  28. '=======================================
  29. 'ObjectClass=user
  30. 'ObjectCategory=person
  31. 'CN
  32.  
  33. strFilter = "(&(objectCategory=person)(objectClass=user)(cn=Juan*))"
  34.  
  35. 'EXAMPLES OF FILTER WHICH IS REALLY JUST A QUERY
  36. '===========================================================
  37. '"(&(objectCategory=person)(objectClass=user))"
  38. '"(&(objectCategory=person)(objectClass=user)(cn=Joe*))"
  39. '"(objectCategory=computer)"
  40. '===========================================================
  41.  
  42.  
  43. ' Comma delimited list of attribute values to retrieve.
  44. strAttributes = "sAMAccountName,cn,distinguishedName, mail, lastlogon"
  45. 'ATTRIBUTES
  46. '===========================================================
  47. 'SAMAccountName     CN          DistinguishedName
  48. 'mail               company     givenName               sn
  49. 'ADsPath            name        sAMAccountName          telephoneNumber
  50.  
  51.  
  52. ' Construct the LDAP syntax query.
  53. strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
  54. adoCommand.CommandText = strQuery
  55. adoCommand.Properties("Page Size") = 100
  56. adoCommand.Properties("Timeout") = 30
  57. adoCommand.Properties("Cache Results") = False
  58.  
  59.  
  60.  
  61. ' Run the query.
  62. Set adoRecordset = adoCommand.Execute
  63.  
  64.  
  65. ' Enumerate the resulting recordset.
  66. Do Until adoRecordset.EOF
  67.  
  68.     ' Retrieve values and display.
  69.     strName = adoRecordset.Fields("sAMAccountName").Value
  70.  
  71.     strCN = adoRecordset.Fields("cn").Value
  72.  
  73.     strLastLogin = adoRecordset.Fields("Lastlogon").Value
  74.  
  75.     'Wscript.Echo "NT Name: " & strName & ", Common Name: " & strCN
  76.     Debug.Print "NT Name: " & strName & ", Common Name: " & strCN & "-" & strLastLogin
  77.     ' Move to the next record in the recordset.
  78.     adoRecordset.MoveNext
  79. Loop
  80.  
  81.  
  82.  
  83. ' Clean up.
  84.  
  85. adoRecordset.Close
  86.  
  87. adoConnection.Close
  88.  
  89.  
  90.  
  91. End Function
  92.  
Sep 8 '07 #1
Share this Question
Share on Google+
1 Reply


maxamis4
Expert 100+
P: 295
In my efforts to figure this out I have found that Microsft does not have this feature most admins use scripts that go back and point the user back to the actual computer he or she is logged into. If anyone knows how to figure this out please let me know thanks
Nov 9 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.