Re: Read Active Directory

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Ed Wyche wrote:

Is there away to read AD and create a report of just the groups and also
if it is a security group or a distribution group? I would appreaciate
some sample code.

You can use ADO to retrieve information on all groups in AD. I have an
example VBScript program that documents all groups in the domain linked
here:

http://www.rlmueller.net/Document%20Domain%20Groups.htm

This program documents the group names, group type, and the direct members.
A simpler script to just document group NetBIOS names and group type could
be as follows:
=========
Option Explicit

Dim adoConnection, adoCommand, objRootDSE, strDNSDomain, strQuery
Dim adoRecordset, strNTName, strGroupType

' Use ADO to search Active Directory.
Set adoConnection = CreateObject("ADODB.Connection")
Set adoCommand = CreateObject("ADODB.Command")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection

' Determine the DNS domain from the RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE";)
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Search for all groups, return the NT name and type of each.
strQuery = "<LDAP://"; & strDNSDomain & ">;" _
& "(objectCategory=group);sAMAccountName,groupType;subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

' Enumerate recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
strNTName = adoRecordset.Fields("sAMAccountName").Value
strGroupType = GetType(adoRecordset.Fields("groupType").Value)
Wscript.Echo strNTName & "(" & strGroupType & ")"
adoRecordset.MoveNext
Loop
adoRecordset.Close
adoConnection.Close

Function GetType(ByVal intType)
' Function to determine group type from the GroupType attribute.
If ((intType And &h01) <> 0) Then
GetType = "Built-in"
ElseIf ((intType And &h02) <> 0) Then
GetType = "Global"
ElseIf ((intType And &h04) <> 0) Then
GetType = "Local"
ElseIf ((intType And &h08) <> 0) Then
GetType = "Universal"
End If
If ((intType And &h80000000) <> 0) Then
GetType = GetType & "/Security"
Else
GetType = GetType & "/Distribution"
End If
End Function
============
More information on using ADO to retrieve information from AD here:

http://www.rlmueller.net/ADOSearchTips.htm

All of the above is VBScript, but the code works fine in VB. However, you
can use early binding for many of the objects. The ADO objects require a
reference to "Microsoft ActiveX Data Objects 2.x library". Then the objects
can be declared similar to:

Dim adoConnection As ADODB.Connection
Dim adoCommand As ADODB.Command
Dim adoRecordset As ADODB.Recordset

Set adoConnection = New ADODB.Connection
Set adoCommand = New ADODB.Command

If you bind directly to AD objects, like user, group, or computer objects,
add a reference to "Active DS Type Library", which is activeds.tlb. Then you
can declare objects similar to:

Dim objUser As IADsUser
Dim objGroup As IADsGroup
Dim objComputer As IADsComputer

and use the appropriate IADs interface.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--



.



Relevant Pages

  • Re: account created attribute in AD
    ... You can use ADO in a VBScript program. ... Dim strBase, strFilter, strAttributes, strQuery, adoRecordset ... Set adoConnection = CreateObject ... ' Construct the LDAP query. ...
    (microsoft.public.windows.server.active_directory)
  • Re: mailmerge and sql
    ... means that you will not be able to see them in a database you open using the ... I believe you may have to use DAO instead of ADO to ... then creates a View containing a UNION query. ... Dim oCatalog As ADOX.Catalog ...
    (microsoft.public.word.mailmerge.fields)
  • Requery of Listbox does not display new data
    ... add a record to the database. ... Then the Lisbox control's requery method is ... The ADO command is run using a connection string to the mdb containing the ... Dim cmd As ADODB.Command ...
    (microsoft.public.access.formscoding)
  • Re: Member or Data Member not Found
    ... you've removed the reference to ADO. ... your declaration for tdf is incorrect. ... 2002 only references ADO, but you can add a reference to DAO. ... You must disambiguate as Dim rst As DAO.Recordset. ...
    (microsoft.public.access.formscoding)
  • Re: mailmerge and sql
    ... get data for a Mailmerge using ADO - i.e. you can get get data vcia ADO but ... and pass the Recordset as a parameter to that. ... > that is essentially a database application with a document ... >>Dim oCatalog As ADOX.Catalog ...
    (microsoft.public.word.mailmerge.fields)