
September 8th, 2007, 10:22 PM
|
 |
Excel at any cost
|
|
Join Date: May 2005
Location: metropolitan area
Posts: 229
  
Time spent in forums: 1 Day 16 h 22 m 16 sec
Reputation Power: 6
|
|
|
LDAP Query to find a users computer
I am doing this in Access, but its more VB than anything else so figured it would be beter to post here.
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
Code:
Function Get_User_Name_AD()
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strLastLogin
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
'KNOWN OBJECTS
'=======================================
'ObjectClass=user
'ObjectCategory=person
'CN
strFilter = "(&(objectCategory=person)(objectClass=user)(cn=Juan* ))"
'EXAMPLES OF FILTER WHICH IS REALLY JUST A QUERY
'================================================= ==========
'"(&(objectCategory=person)(objectClass=user))"
'"(&(objectCategory=person)(objectClass=user)(cn=Joe*) )"
'"(objectCategory=computer)"
'================================================= ==========
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,distinguishedName, mail, lastlogon"
'ATTRIBUTES
'================================================= ==========
'SAMAccountName CN DistinguishedName
'mail company givenName sn
'ADsPath name sAMAccountName telephoneNumber
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
strName = adoRecordset.Fields("sAMAccountName").Value
strCN = adoRecordset.Fields("cn").Value
strLastLogin = adoRecordset.Fields("Lastlogon").Value
'Wscript.Echo "NT Name: " & strName & ", Common Name: " & strCN
Debug.Print "NT Name: " & strName & ", Common Name: " & strCN & "-" & strLastLogin
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
End Function
|