[AccessD] How do I query Active Directory?

MartyConnelly martyconnelly at shaw.ca
Fri Mar 17 14:10:29 CST 2006


You could try code samples like this. Most of this type of code using 
LDAP, ADSI and WMI
is written in VBScript but is easily modifiable into VBA. Just define 
the objects and variants.
Look around this site with 100's of sample vbscripts
http://www.activexperts.com/activmonitor/windowsmanagement/scripts/activedirectory/computer/#ListAllComputer.htm

Sub testnames()

'List All Computer Accounts in Active Directory
'Returns the name and location for all the computer accounts in Active 
Directory.
Const ADS_SCOPE_SUBTREE = 2
Dim objConnection As Object
Dim objCommand As Object
Dim objrecordset As Object
'points at domain fabrikam.com
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCommand.ActiveConnection = objConnection
'note sample domain below is fabrikam.com
objCommand.CommandText = _
    "Select Name, Location from 'LDAP://DC=fabrikam,DC=com' " _
        & "Where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objrecordset = objCommand.Execute
objrecordset.MoveFirst

Do Until objrecordset.EOF
    Debug.Print "Computer Name: " & objrecordset.Fields("Name").Value
    Debug.Print "Location: " & objrecordset.Fields("Location").Value
    objrecordset.MoveNext
Loop
Set objrecordset = Nothing
Set objConnection = Nothing
Set objCommand = Nothing
End Sub




Sub test()
' You can get the creation date for each account from Active
'Directory. Every AD object has a WhenCreated and WhenChanged
'attribute. You can dump these attributes into a flat file using
'the LDIFDE utility, or you can dump them into a comma-delimited
'file using CSVDE (both utilities come with Windows 2000).

'Here 's the syntax to dump the two attributes for the user
'objects in an OU called Phoenix in a domain called Company.com
'to the console for viewing (the entire entry should typed as a
'single line):

'     ldifde -d ou=phoenix,dc=company,dc=com -l whencreated,
'     whenchanged -p onelevel -r "(ObjectCategory=user)"
'     -f con

'If you wanted to save the dump to a file, change the "-f" switch
'from "con" to a file name.

'The last logon timestamp uses this format: YYYYMMDDHHMMSS, with
'the hour shown in Universal Coordinated Time. A time stamp of
'20040115182937.0Z corresponds to Jan 15 2004 18:29:37 UCT.

'USRSTAT is slow, and the report you get has to be merged with
'the LDIFDE dump. So, I put together a script that searches for
'user objects at each domain controller, then lists the local
'logon time and the creation time. The user logon timestamp
'requires conversion from a long integer. I borrowed the
'conversion code comes from Richard L. Mueller
'( http://www.rlmueller.net/Programs ). Richard's full script
'also takes the local time zone from the Registry and converts
'the time from UCT to local time. Nifty.
'Establish ADO Constants
 Const ADS_CHASE_REFERRALS_NEVER = &O0
 Const ADS_CHASE_REFERRALS_SUBORDINATE = &O20
 Const ADS_CHASE_REFERRALS_EXTERNAL = &O40
 Const ADS_CHASE_REFERRALS_ALWAYS = &O60
 Const ADS_SCOPE_BASE = 0
 Const ADS_SCOPE_ONELEVEL = 1
 Const ADS_SCOPE_SUBTREE = 2
 Dim RootDSE As Object
 Dim domainDN As Object
 Dim Connection As Object
 Dim dc As Object
 Dim dcList As Object
 Dim rs As ADODB.Recordset
 Dim adoLastLogon As Object
 Dim logondate As Object
 Dim longdate As Object
 Dim longDateHigh As Long
  Dim longDateLow As Long
  Dim oNet As Object
  Set oNet = CreateObject("Wscript.Network")
  Debug.Print oNet.UserName

 'Get Distinguished Name for local domain
 'Set RootDSE = GetObject("LDAP://RootDSE")
 'Set RootDSE = GetObject("LDAP://MARTIN")
 Set RootDSE = GetObject("LDAP://marty")
 domainDN = RootDSE.Get("DefaultNamingContext")

 'Initialize ADO connection
 Set Connection = CreateObject("ADODB.Connection")
 Connection.Provider = "ADsDSOObject"
 Connection.Open
 Set Command = CreateObject("ADODB.Command")
 Set Command.ActiveConnection = Connection
 Command.Properties("Page Size") = 1000
 Command.Properties("Timeout") = 30
 Command.Properties("searchscope") = ADS_SCOPE_SUBTREE
 Command.Properties("Chase referrals") = ADS_CHASE_REFERRALS_NEVER
 Command.Properties("Cache Results") = False
 
 'Get list of domain controllers for the domain
 Set dcList = GetObject("LDAP://ou=domain controllers," & _
    domainDN)
 
 'Walk each domain controller for logons
 For Each dc In dcList
 Debug.Print String(40, "=")
 Debug.Print "Logon dates at " & dc.DNSHostName
 
 Command.CommandText = "SELECT name,lastlogon," & _
    "whencreated,whenchanged FROM " & _
    "'LDAP://" & dc.DNSHostName & "/" & _
    domainDN & "' WHERE objectcategory = 'user'"
 
 Set rs = Command.Execute
 Do Until rs.EOF
    adoLastLogon = rs.Fields("lastlogon")
    On Error Resume Next
    Err.Clear
    Set longdate = adoLastLogon
    If Err.Number <> 0 Then
       Err.Clear
       logondate = "No Local Logon"
    Else
       longDateHigh = longdate.HighPart
       longDateLow = longdate.LowPart
       If (longDateLow = 0) And (longDateHigh = 0) Then
          logondate = "No Local Logon"
       Else
          If longDateLow < 0 Then longDateHigh = longDateHigh + 1
          logondate = #1/1/1601# + (((longDateHigh * (2 ^ 32)) _
             + longDateLow) / 600000000 / 1440)
       End If
    End If
 
    Debug.Print "User Name: " & rs.Fields("name")
    Debug.Print " Last logon: " & logondate
    Debug.Print " Object Created: " & rs.Fields("WhenCreated")
    Debug.Print " Object Modified: " & rs.Fields("WhenChanged")
 
    rs.MoveNext
 Loop
 
 'Debug.Print vbNL
 Next
 
' WScript.Quit()
 
'As you mentioned, Zev, Windows Server 2003 has an additional
'attribute called LastLogonTimestamp that replicates to every
'domain controller once you shift to a Windows Server 2003
'functional level. You can rewrite this script to search for
'the contents of LastLogonTimestamp on any domain controller.
End Sub

Lonnie Johnson wrote:

>Does anyone have code or a model of a procedure that will allow me to query members of the Active Directory?
>
>
>May God bless you beyond your imagination!
>Lonnie Johnson
>ProDev, Professional Development of MS Access Databases
>Visit me at ==> http://www.prodev.us
>
>
>
>
>
> 
>
>
>
>
>
>		
>---------------------------------
>Yahoo! Travel
> Find  great deals to the top 10 hottest destinations!
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada






More information about the AccessD mailing list