Skip to sidebar Skip to content
Ipercube Technical
Jack of all trades, master of none
Date: Time: - Stardate:
Welcome, Log in by clicking  Here!
SS64
MDN
Stack
Code
GitHub
Wiki
Convert
V Studio
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions
Ipercube Technical
  • Home
  • VB Functions
    • Access Functions
    • Assembly Info
    • Color/Image Functions
    • Data Table Functions
    • Email Functions
    • Active Directory Functions
    • LDAP Functions
    • Excel Functions
    • Logging Functions
    • Source Code Retrieval
    • String Functions
    • Sql Functions
    • Object Utilities Functions
    • System and Utilities
    • Utility Forms
  • VB Classes
    • Environment Variables Helper
  • ABAP
    • ABAP Reports
    • ABAP HTTP Functions

Get All Users From Groups Pattern

Retrieves all user accounts that are members of Active Directory groups whose names match the specified pattern.

  • Uses a DirectoryServices.AccountManagement.PrincipalSearcher and DirectoryServices.AccountManagement.GroupPrincipal to identify groups matching the specified pattern.
  • For each matching group, calls iuADFunctions.adGetAllGroupUsers(String,String,String,String) to get the users and adds them to the result list.
  • Removes duplicate users based on DistinguishedName by using a custom iuADFunctions.adUserPrincipalComparer comparer.
  • Any errors during retrieval of group members for a specific group are logged and do not stop the process.
VB
    Public Function adGetAllUsersFromGroupsPattern(groupPattern As String, 
                                                   domain As String, 
                                                   username As String, 
                                                   password As String) As List(Of UserPrincipal)
                                                   
        Dim allUsers As New List(Of UserPrincipal)()

        ' Create the PrincipalContext
        Dim context As New PrincipalContext(ContextType.Domain, domain, username, password)

        ' Create a GroupPrincipal filter using the name pattern
        Dim groupFilter As New GroupPrincipal(context)
        groupFilter.Name = groupPattern

        ' Use PrincipalSearcher to find all matching groups
        Dim searcher As New PrincipalSearcher(groupFilter)
        Dim groups As List(Of GroupPrincipal) = searcher.FindAll().OfType(Of GroupPrincipal)().ToList()

        ' For each matching group, call the existing function and merge the results
        For Each group As GroupPrincipal In groups
            Dim groupDN As String = group.DistinguishedName

            Try
                Dim users = adGetAllGroupUsers(groupDN, domain, username, password)
                allUsers.AddRange(users)
            Catch ex As Exception
                ' Log error and continue with the next group
                Debug.WriteLine($"Error for group {groupDN}: {ex.Message}")
            End Try
        Next

        ' Remove duplicate users based on DistinguishedName
        allUsers = allUsers.Distinct(New adUserPrincipalComparer()).ToList()

        Return allUsers
    End Function
Posted by ipercube


© 2026 Ipercube Design