Results 1 to 40 of 40

Thread: Collecting all AD groups a user is member of, including nested ones

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2010
    Posts
    21

    Question Collecting all AD groups a user is member of, including nested ones

    Dear all,

    I've created a function that works very well, but it's limited to AD groups direct membership. I would like to display all AD groups a user belongs to, included the nested one.

    Here is my code:
    vb Code:
    1. Sub CollectGroupMemberShip()
    2.         'Reset the form to default values
    3.         ResetToDefault()
    4.  
    5.         Dim strGroupList = Nothing
    6.  
    7.         'Variable that will be used to cancel the rest of the process in case wrong username
    8.         UserNotFound = 0
    9.  
    10.         'Various Dim to connect to AD using wished domain and user account
    11.         Dim rootDSE As New DirectoryEntry("LDAP://MyDomain/RootDSE")
    12.         Dim filterString As String = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" & UserLoginID_TextBox.Text & "))"
    13.         Dim domainRoot As New DirectoryEntry("LDAP://MyDomain/" & rootDSE.Properties("defaultNamingContext")(0).ToString())
    14.         Dim domainSearch As New DirectorySearcher(domainRoot, filterString)
    15.  
    16.         'Property to search for
    17.         domainSearch.PropertiesToLoad.Add("memberOf")
    18.  
    19.         Dim domainSearchResult As SearchResult = domainSearch.FindOne()
    20.  
    21.         'In case the username is not found, raise an error and quit
    22.         If domainSearchResult Is Nothing Then
    23.             MsgBox("User not found. Please check the spelling!", MsgBoxStyle.Critical, "User not found...")
    24.             UserNotFound = 1
    25.             Exit Sub
    26.         End If
    27.  
    28.         'For each group the user is member of, I isolate the name of the group and I add it into a list
    29.         For Each domainGroup In domainSearchResult.Properties("memberof")
    30.             'Split the full path using 'comma'
    31.             Dim Split1 = Split(domainGroup, ",")
    32.             'Split the first occurance using 'equal' to get rid of 'CN='
    33.             Dim Split2 = Split(Split1(0), "=")
    34.             'Dim the group name (OK not really necessary, but nicer for using it afterwards)
    35.             Dim GroupName = Split2(1)
    36.             'Now time to use the group list
    37.             If strGroupList = "" Then
    38.                 strGroupList = GroupName
    39.             Else
    40.                 strGroupList = strGroupList & "," & GroupName
    41.             End If
    42.         Next
    43.         'Split the group list using 'comma'
    44.         Dim arrGroupList = Split(strGroupList, ",")
    45.         'Using 'QuickSort' function to sort the list by alphabetical order
    46.         Quicksort(arrGroupList, LBound(arrGroupList), UBound(arrGroupList))
    47.         'Re-arrange the list to have one group per line
    48.         Dim strSortedGroups = Join(arrGroupList, vbCrLf)
    49.         'Display the result in the textbox on the form
    50.         UserGroups_TextBox.Text = strSortedGroups
    51.  
    52.         'Just display on the form the number of groups the user is member of
    53.         Groups_GroupBox.Text = Groups_GroupBox.Text & " (" & arrGroupList.Length & ")"
    54.  
    55.     End Sub

    Is there any ways to achieve that?

    Many thanks in advance for your answer(s)
    Mezzomix23
    Last edited by mezzomix23; Sep 1st, 2010 at 10:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width