I am trying to get a users group membership and limiting the results to those that match a string, ie I am only interested in the users group membership where the group begins with "test-".
The following is what I have been playing around with, even though the user is apart of several groups that match the search string, the If statement is not returning True on any of them.
Private Function GetGroups(ByVal userName As String) As Collection
Dim Groups As New Collection
Dim intCount As Integer
Dim entry As DirectoryEntry = ADEntry()
Dim mySearcher As DirectorySearcher = New DirectorySearcher(entry)
Dim arrList As New ArrayList()
' Limit the search results to only users
mySearcher.Filter = "(&(ObjectClass=User)(CN=" & userName & "))"
' Set the sort order
mySearcher.PropertiesToLoad.Add("MemberOf")
Dim searchResults As SearchResultCollection = mySearcher.FindAll()
MessageBox.Show(searchResults.Count)
If searchResults.Count > 0 Then
Dim group As New DirectoryEntry(searchResults(0).Path)
For Each member As Object In group.Properties("MemberOf")
MessageBox.Show("Pre: "+ member) 'This message box returns all the groups the user is apart of.
If group.Properties("memberOf").Contains("test-") = True Then
MessageBox.Show(member) ' This message box never shows
End If
Next
End If
Return Groups
End Function
Is there any way of applying a search or If statement agains an Object where the constraint is a wildcard?
The groups I am looking for could be one of about 60 (this amount does increase and decrease as staff leave).
I am using VB.NET 2.0.
Thanks,
Matt