troubleshooting Question

How can I get a list of AD groups that match a partial name?

Avatar of Elliott Ward
Elliott Ward asked on
Visual Basic.NETActive Directory
5 Comments1 Solution1903 ViewsLast Modified:
I need to search AD for any groups that match a partial name, then search each group for the users inside it.

I have the code to list the users in a group, listed below.  But how do I get the listing of the groups to search?

Public Function UsersInActiveDirectoryGroup(ByVal GroupName As String) As Specialized.StringCollection
   Dim members As New Specialized.StringCollection

       Dim domainContext As New ActiveDirectory.DirectoryContext(ActiveDirectory.DirectoryContextType.Domain)
       Using dc As ActiveDirectory.DomainController = ActiveDirectory.DomainController.FindOne(domainContext)
           Using pctx As New PrincipalContext(ContextType.Domain, dc.Domain.Name)
                Using grp As GroupPrincipal = GroupPrincipal.FindByIdentity(pctx, IdentityType.Name, GroupName)
                    If Not IsNothing(grp) Then
                        For Each p As Principal In grp.GetMembers(True)
                     End If
                 End Using
             End Using
         End Using
         Return members
     Catch ex As Exception
         Return Nothing
     End Try
 End Function
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros