Elliott Ward
asked on
How can I get a list of AD groups that match a partial name?
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 UsersInActiveDirectoryGrou p(ByVal GroupName As String) As Specialized.StringCollecti on
Dim members As New Specialized.StringCollecti on
Try
Dim domainContext As New ActiveDirectory.DirectoryC ontext(Act iveDirecto ry.Directo ryContextT ype.Domain )
Using dc As ActiveDirectory.DomainCont roller = ActiveDirectory.DomainCont roller.Fin dOne(domai nContext)
Using pctx As New PrincipalContext(ContextTy pe.Domain, dc.Domain.Name)
Using grp As GroupPrincipal = GroupPrincipal.FindByIdent ity(pctx, IdentityType.Name, GroupName)
If Not IsNothing(grp) Then
For Each p As Principal In grp.GetMembers(True)
members.Add(p.SamAccountNa me)
Next
End If
End Using
End Using
End Using
Return members
Catch ex As Exception
Return Nothing
End Try
End Function
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 UsersInActiveDirectoryGrou
Dim members As New Specialized.StringCollecti
Try
Dim domainContext As New ActiveDirectory.DirectoryC
Using dc As ActiveDirectory.DomainCont
Using pctx As New PrincipalContext(ContextTy
Using grp As GroupPrincipal = GroupPrincipal.FindByIdent
If Not IsNothing(grp) Then
For Each p As Principal In grp.GetMembers(True)
members.Add(p.SamAccountNa
Next
End If
End Using
End Using
End Using
Return members
Catch ex As Exception
Return Nothing
End Try
End Function
I'm not too familiar with this kind of code in general, but the main thing you would add is:
samAccountName=*BLAH*
where "BLAH" is the name you want it to match.
samAccountName=*BLAH*
where "BLAH" is the name you want it to match.
Dont know how big of a userbase you got . but i normally use the manage engine ad manager plus to get all kinds of reports . They also have a free version for 100 users or so ..
ADManager Plus- Active Directory Management & Reporting
http://www.manageengine.com/products/ad-manager/
Ad Manager Plus
ADManager Plus- Active Directory Management & Reporting
http://www.manageengine.com/products/ad-manager/
Ad Manager Plus
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks for your help.
http://www.codeproject.com/KB/system/everythingInAD.aspx