Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AD - Export memberdata of SEVERAL groups at once.

Status
Not open for further replies.
May 28, 2003
106
GB
Hi All,

our Distribution List OU in AD has about 46 Distribution lists and contains another container that also contains about 30 distribution list. Most of them have slightly different names (i.e Office Corp and Corp Office) but serve the same purpose. In other words a BIG Mess.

I would like to export all groups into a txt/xls/html file that should display the group and its members. So I can sort out this mess.

I know that you can get the members of a group via dsquery/dsget. But then I would have to do that for all 80 groups.

All ideas higly appreciated and many thanks in advance,
Sebastian
 
You can do it with a simple VB Script. Let me know whether you want to export the groups in an OU or from the entire domain. Based on this I will try to draft a script.

-Keshav
 
Vbscript is definatley the way to go.

Code:
[green]'==========================================================================
'
' NAME: GetGroupMembers.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]
' DATE  : 3/23/2005
'
' COMMENT: Dumps group members to text file.
'          This script and many more are available in
'          the Spider's Parlor Admin Script Pack 
'          [URL unfurl="true"]http://www.thespidersparlor.com/vbscript[/URL]
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'==========================================================================
[/green]
Dim sResultText,Grps,MemberList
Dim oRootDSE, oConnection, oCommand, oRecordSet
Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = oConnection

ldstring = "<LDAP://" & oRootDSE.get("defaultNamingContext") & ">;" 

objCommand.CommandText=ldstring & "(objectClass=group);name,SamAccountName"

Set oRecordSet = objCommand.Execute()
Do While Not oRecordSet.EOF
	sResultText = sResultText & oRecordSet.Fields("samAccountName") & vbCrLf
	'WScript.Echo oRecordSet.Fields("samAccountName") & vbCrLf
	MemberList=RetrieveUsers(dom,oRecordSet.Fields("samAccountName"))
	'WScript.Echo Memberlist
	sResultText = sResultText & memberlist & vbCrLf & "************************************" & vbCrLf
	
	oRecordSet.MoveNext
Loop


Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile (dom & "DomainGroupUsers.txt", ForWriting)
ts.write sResultText
MsgBox "Done"

[green]
'*****************************************************************************************
'*****************************************************************************************[/green]
Function RetrieveUsers(domainName,grpName)

dim dom
dim grp
dim GrpObj
dim mbrlist
dim mbr
[green]
'-------------------------------------------------------------------------------
' *** Enumerate Group Members ***
'-------------------------------------------------------------------------------
[/green]
grp = grpName
Set objDomain = getObject("LDAP://rootDse")
domainName = objDomain.Get("dnsHostName")
[green]' Build the ADSI query and retrieve the group object[/green]
Set GrpObj = GetObject("WinNT://" & domainName & "/" & grp & ",group")
[green]
' Loop through the group membership and build a string containing the names[/green]
for each mbr in GrpObj.Members
   On error resume next
   mbremail = SearchEmail(mbr.name)  
   If Err Then
	   mbrlist = mbrlist & vbTab & mbr.name & vbCrLf
   Else[green]
   'if you don't want the email addresses, then copy the line 2 up to below[/green]
	   mbrlist = mbrlist & vbTab & mbr.name & vbTab & vbTab & mbremail+ vbCrLf
   End If
Next
[green]
'The next line returns mbrlist back up to the main body[/green]
RetrieveUsers=mbrlist

End Function

Public Function SearchEmail(ByVal vSAN)[green]
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name[/green]
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));mail;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchEmail = oRecordSet.Fields("mail")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Perfect!

Hope the above script by Mark will help you in your work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top