Re: Listing Group Members

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



This is a script I use to enumerate the groups in my OU. You may be
able to modify it to suit your own purposes. Most of it was put
together using snippets found on the web so I don't claim it to be my
own (credit probably goes in part to some of the regulars here).
It doesn't account for nested groups as my domain is still in mixed
mode but it should be easy enough to modify if you need to enumerate
nested groups as well. To output to a text file just redirect the
output from the command console. I use tabs as seperators so that it
can be easily imported into excel if required. Other than changing the
constant for the OU you shouldn't need to modify it much to get it to
work.


Option Explicit


Dim objMemberList
Const MyOUDN = "OU=MyOU,DC=MyOrg,DC=MyCorp,DC=com"


main


'#######################################################################################
Sub main
Dim objOU
Dim objGroup, strGroup, iGroupCount

' Dictionary object to track groups.
Set objMemberList = CreateObject("Scripting.Dictionary")
objMemberList.CompareMode = vbTextCompare

' Bind to base OU.
Set objOU = GetObject("LDAP://"; & MyOUDN)

' Filter on groups directly in OU.
objOU.Filter = Array("group")

' Enumerate groups.
iGroupCount = 0
For Each objGroup In objOU
iGroupCount = iGroupCount + 1
strGroup = objGroup.sAMAccountName
WScript.Echo UCASE(objGroup.sAMAccountName)
Call EnumGroup(objGroup)
Next

WScript.Echo "Total Number of Groups = " & iGroupCount

set objOU = nothing


End sub



'#######################################################################################
Sub EnumGroup(ByVal objADGroup)
Dim objMember, iCount

' Check if group already enumerated.
If (objMemberList.Exists(objADGroup.sAMAccountName) = False) Then
' Add this group to dictionary object.
objMemberList.Add objADGroup.sAMAccountName, True
iCount = 0
For Each objMember In objADGroup.Members
iCount = iCount + 1
' Check if member is a group.
If (UCase(Left(objMember.objectCategory, 8)) = "CN=GROUP") Then
' Call EnumGroup(objMember)
Else
If objMember.AccountDisabled then
wscript.Echo vbtab & objMember.sAMAccountName & vbtab &
objMember.DisplayName & vbtab & "(Account Disabled)."
Else
wscript.Echo vbtab & objMember.sAMAccountName & vbtab &
objMember.DisplayName
End if
End If
Next
If iCount = 0 Then
WScript.Echo vbtab & "Group is empty."
End if
End If

End Sub


.



Relevant Pages

  • Re: Script to convert the group type from Domain local to Universal
    ... you modify the groupType attribute of the group objects. ... ' Enumerate all groups in this container and sub containers. ...
    (microsoft.public.windows.server.active_directory)
  • Re: scared about refrences...
    ... functions should not modify their caller's data. ... for index, value in enumerate: ... But notice that you only need a shallow copy, not a deep copy, because you ... If so, please post the exception. ...
    (comp.lang.python)
  • Re: scared about refrences...
    ... functions should not modify their caller's data. ... def print_list: ... for index, value in enumerate: ... If so, please post the exception. ...
    (comp.lang.python)
  • Re: Listing Group Members
    ... mode but it should be easy enough to modify if you need to enumerate ... nested groups as well. ... Indeed, just uncomment one line: ...
    (microsoft.public.scripting.vbscript)