Solved

VBscript query to get group member information for multiple domains in a forest.

Posted on 2008-10-22
2
6,533 Views
Last Modified: 2012-08-13
I am updating a script that we have that goes into AD and extracts all email enabled users, groups and contacts and dumps them to a CSV file.  I am having an issue now because we have an additional child domain in our forrest.  Need to be able to run this script and create one CSV file that contains the forest users and distribution groups and contacts.  

The problem is that when I do a query of the GC i only get the members of groups not in the domain.  To get the groups of the domain I am in I can query LDAP instead but then I don't get the other domain's group members.  I am lookin for a solution that is not going to double the code.

'Attach to Active Directory
set objRoot = GetObject("LDAP://rootDSE")
strDNS = objRoot.Get("defaultNamingContext")
strRootDNS = objRoot.Get("rootDomainNamingContext")
 
set objConnection = CreateObject("ADODB.Connection")
ObjConnection.Open "Provider=ADsDSOObject;"
 
set objCommand = CreateObject("ADODB:Command")
objCommand.ActiveConnection = objConnection
 
objCommand.Properties ("Page Size")= 500      	'500 results at a time
objCommand.Properties ("Cache Results")= False  'Reduce Client mem Consumption
 
'=====Query Global Catalog======
objCommand.CommandText = "<GC://" & strRootDNS &">;" & _
"(&(objectCategory=group)(objectClass=group));" & _
"name,cn,SAMAccountName,mail,givenName,sn,displayName," & _
"proxyAddresses,member,mailNickname;subtree"
 
set objRecordset = objCommand.Execute
 
inNumGroups = 0
 
Wscript.Echo ("Pre-Processing (objectCatagory=Group)(objectClass=Group) Data...")
 
'===Write information to Export File===
While Not objRecordSet.EOF
    'working code
 
 arrMember = objRecordSet.Fields("member")
For each strMember in arrMember
	'workingcode
Next
 
objRecordSet.MoveNext
wend

Open in new window

0
Comment
Question by:JeffCothern
2 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 22778312
Place the constanst

ADS_SCOPE_SUBTREE = 2

at the start of your script.

Then alter this section of your code:
set objCommand = CreateObject("ADODB:Command")
objCommand.ActiveConnection = objConnection

to read:

set objCommand = CreateObject("ADODB:Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 22832926
My script first builds an array of domain names.  Next, the script will loop through each domain and continue executing code.

I changed your code a little to check to see if the group has members and also added "primaryGroupID" as a field to query.  It is important to note that a users primary group is listed at the user level and they are not recorded as members of that particular group in the traditional sense.  Take for example the Domain Users group.  If you run the code as is, the domain users group will not contain everyone you would expect.  Because the primary group is held within the user accounts, it is foreseeable that you will also have to pull user data as well.

In the end, I imagine doubling the code is entirely possible.
'http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_23837470.html
 
'Flush out all domain names found in AD and add them to arrDomainNames
set objRootDSE   = GetObject("LDAP://RootDSE")
strBase          = "<LDAP://cn=Partitions," & objRootDSE.Get("ConfigurationNamingContext") & ">;"
strFilter        = "(&(objectcategory=crossRef)(systemFlags=3));"
strAttrs         = "name,trustParent,nCName,dnsRoot,distinguishedName;"
strScope         = "onelevel"
set objConn      = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
objRS.MoveFirst
 
set arrDomainNames     = CreateObject("Scripting.Dictionary")
set dicDomainHierarchy = CreateObject("Scripting.Dictionary")
set dicDomainRoot      = CreateObject("Scripting.Dictionary")
 
while not objRS.EOF 
    dicDomainRoot.Add objRS.Fields("name").Value, objRS.Fields("nCName").Value
        if objRS.Fields("trustParent").Value <> "" then
            arrDomainNames.Add objRS.Fields("name").Value, 0
            set objDomainParent = GetObject("LDAP://" & objRS.Fields("trustParent").Value)
            dicDomainHierarchy.Add objRS.Fields("name").Value,objDomainParent.Get("name")
       else 
            arrDomainNames.Add objRS.Fields("name").Value, 1
       end if
       objRS.MoveNext
wend
 
'Attach to Active Directory
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
 
for each strDomain in arrDomainNames
    strBase = "<LDAP://" & strDomain & ">"
    strFilter = "(&(objectCategory=group)(objectClass=group))"
    strAttributes = "name,cn,SAMAccountName,mail,givenName,sn,displayName,proxyAddresses,member,primaryGroupID,mailNickname"
    strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
    adoCommand.CommandText = strQuery
    adoCommand.Properties("Page Size") = 100
    adoCommand.Properties("Timeout") = 30
    adoCommand.Properties("Cache Results") = False
    adoCommand.Properties("Sort On") = "cn"
    Set adoRecordset = adoCommand.Execute
    inNumGroups = 0
    Wscript.Echo ("Pre-Processing " & strFilter & " Data...")
    '===Write information to Export File===
    While Not adoRecordset.EOF
        'working code
        arrMember = adoRecordset.Fields("member")
        if IsArray(arrmember) then
            For each strMember in arrMember
                'workingcode
                msgbox adoRecordset.Fields("cn") & vbCRLF & strMember
            Next
        else
            msgbox adoRecordset.Fields("cn") & vbCRLF & "This group has no members"
        end if
        adoRecordset.MoveNext
    wend
Next

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question