Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim objRootDSE, strDNSDomain, strQuery, adoRecordset, strName,groupType,groupName,iRow
Dim objExcel,arrMembers, strMember
' 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 distribution groups.
strFilter = "(objectCategory=group)"
' Comma delimited list of attribute values to retrieve.
strAttributes = "distinguishedName,member,groupType,name"
' 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
Set objExcel = CreateObject("Excel.Application")
With objExcel
.SheetsInNewWorkbook = 1
.Workbooks.Add
.Visible = True
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
strName = adoRecordset.Fields("distinguishedName").Value
groupType = adoRecordset.Fields("groupType").Value
groupName = Replace(adoRecordset.Fields("name").Value,"CN=", "")
'get only distribution groups
if groupType=2 or groupType=4 or groupType=8 then
irow=1
.ActiveWorkbook.Worksheets.Add
.ActiveSheet.Name= Left(groupName, 31)
arrMembers = adoRecordset.Fields("member").Value
'Wscript.Echo "Distribution Group: " & strName
If IsNull(arrMembers) Then
'Wscript.Echo "-- <No Members>"
Else
For Each strMember In arrMembers
'Wscript.Echo "-- " & strMember
Set objRootDSE = GetObject("LDAP://"&strMember)
.Cells(iRow,1) = Replace(objRootDSE.Name,"CN=", "")
irow=irow + 1
Next
End If
End If
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
.Columns(1).entirecolumn.autofit
End With
' Clean up.
adoRecordset.Close
adoConnection.Close
Set objExcel = Nothing
|