Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check that active directory group exists...

Status
Not open for further replies.

lidds

Programmer
Jun 9, 2005
72
GB
I want to let the user enter a active directory group, and I want to verify that the group they entered does exist. However I am not sure how to do this, I assume it is something simular to the below code:

Code:
        Dim de As DirectoryEntry = New DirectoryEntry(Me.strDomain)
        Dim deSearch As DirectorySearcher = New DirectorySearcher()
        deSearch.SearchRoot = de
        deSearch.Filter = "<enter group filter syntax>" & myGroupName & ")"
        Try
            Dim results As SearchResultCollection = deSearch.FindAll()
            If results.Count = 0 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Thanks

Simon
 
There are lots of places in the Active Directory where "group" data can be stored. Use this to find where the group data is stored first. It puts all the variables you can access for a given name in a RichTextBox.

Code:
    Private Sub ActiveDirectoryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActiveDirectoryToolStripMenuItem.Click
        Dim SearchName As String = "Put Name Here" 'System.Environment.UserName
        Dim entry As New DirectoryServices.DirectoryEntry()
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult
        Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection

        rtb1.Clear()

        mySearcher.Filter = ("(anr= " & SearchName & ")")

        For Each result In mySearcher.FindAll
            objCollResultProperty = result.Properties
            For Each obj As Object In objCollResultProperty
                For Each obj2 As Object In obj.Value
                    If TypeOf obj2 Is Array Then
                        For Each obj3 As Object In obj2
                            rtb1.Text &= "-> " & obj.key & " -- " & obj3.ToString & vbCrLf
                        Next
                    Else
                        rtb1.Text &= obj.key & " -- " & obj2.ToString & vbCrLf
                    End If
                Next
            Next
        Next

        MsgBox("Done")
    End Sub
Of course put the name you want to search for in "Put Name Here". You can see this in the code, but first is the variable name then "--" followed by value. "->" means that it is a variable under the variable proceeding it.

Once you find where the group name you are looking for is stored then you can try to find it. Otherwise as far as I know you have to check against every variable to find it. I'm no AD guru so there may be another way to do it I don't know about.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top