How can I get optimize my function?

lazoli

New member
Joined
Jul 27, 2009
Messages
1
Programming Experience
1-3
The following function find all groups in active directory by username.
If I get a Groupname, then I will find if this is subgroup, If I get a
subgroup then i will find if this is a subgroup of an group...

the goal is, i want to search all group with subgroup iterativ...

now i have depth 3, but if I want to search deeper than I will get
a long code.

Is it possible to simplyfy my code?

i hope you understand me ;)

thans a lot

VB.NET:
    Public Function GetActiveDirectoryUserGroupsDS3(ByVal UserName As String) As DataSet

        Dim dsGroup As New DataSet()
        Dim search1 As New DirectorySearcher("")
        Dim search2 As New DirectorySearcher("")
        Dim search3 As New DirectorySearcher("")
        Dim search4 As New DirectorySearcher("")
        Dim search5 As New DirectorySearcher("")
        Dim groupCount1 As Int64
        Dim groupCount2 As Int64
        Dim groupcount3 As Int64
        Dim groupcount4 As Int64
        Dim groupcount5 As Int64
        Dim counter1 As Int64
        Dim counter2 As Int64
        Dim counter3 As Int64
        Dim counter4 As Int64
        Dim counter5 As Int64
        Dim GroupName1 As String
        Dim GroupName2 As String
        Dim GroupName3 As String
        Dim GroupName4 As String
        Dim GroupName5 As String
        Dim GroupArr1 As Array
        Dim GroupArr2 As Array
        Dim GroupArr3 As Array
        Dim GroupArr4 As Array
        Dim GroupArr5 As Array
        Dim PrimaryGroup As String

        'Create a new table object within the dataset
        Dim dtGroup As DataTable = dsGroup.Tables.Add("Groups")
        dtGroup.Columns.Add("Gruppenname")
        dtGroup.Columns.Add("Gruppentyp")

        Try
            search1.Filter = "(&(!(userAccountControl:1.2.840.113556.1.4.803:=2))(objectCategory=user)(samaccountname=" + UserName.ToString.Trim + "))"
            search1.PropertiesToLoad.Add("memberOf")
            Dim result As SearchResult = search1.FindOne()

            If Not (IsNothing(result)) Then

                Try
                    groupCount1 = result.Properties("memberOf").Count
                Catch ex As NullReferenceException
                    groupCount1 = 0
                End Try

                If groupCount1 > 0 Then
                    For counter1 = 0 To groupCount1 - 1
                        GroupName1 = ""
                        GroupName1 = CStr(result.Properties("memberOf")(counter1))

                        GroupArr1 = Split(GroupName1, ",")
                        If Not (IsNothing(GroupArr1(0))) Then
                            GroupName1 = Mid(GroupArr1(0), 4, Len(GroupArr1(0)) - 3)

                            'set a new empty row
                            Dim drGroup As DataRow = dtGroup.NewRow()

                            'populate the column
                            drGroup("Gruppenname") = GroupName1
                            drGroup("Gruppentyp") = getGroupType(GroupName1)

                            'append the row to the table of the dataset
                            dtGroup.Rows.Add(drGroup)

                            ' Suche Unterkategorie
                            search2.Filter = [String].Format("(cn={0})", GroupName1)
                            search2.PropertiesToLoad.Add("memberOf")
                            Dim result2 As SearchResult = search2.FindOne()

                            Try
                                groupCount2 = result2.Properties("memberOf").Count
                            Catch ex As NullReferenceException
                                groupCount2 = 0
                            End Try

                            For counter2 = 0 To groupCount2 - 1

                                GroupName2 = ""
                                GroupName2 = CStr(result2.Properties("memberOf")(counter2))
                                GroupArr2 = Split(GroupName2, ",")
                                If Not (IsNothing(GroupArr2(0))) Then
                                    GroupName2 = Mid(GroupArr2(0), 4, Len(GroupArr2(0)) - 3)

                                    'set a new empty row
                                    Dim drGroup2 As DataRow = dtGroup.NewRow()

                                    'populate the column
                                    drGroup2("Gruppenname") = GroupName2 & " --> " & GroupName1
                                    drGroup2("Gruppentyp") = getGroupType(GroupName1)

                                    'append the row to the table of the dataset
                                    dtGroup.Rows.Add(drGroup2)


                                    ' Suche Unterkategorie
                                    search3.Filter = [String].Format("(cn={0})", GroupName2)
                                    search3.PropertiesToLoad.Add("memberOf")
                                    Dim result3 As SearchResult = search3.FindOne()

                                    Try
                                        groupcount3 = result3.Properties("memberOf").Count
                                    Catch ex As NullReferenceException
                                        groupcount3 = 0
                                    End Try

                                    For counter3 = 0 To groupcount3 - 1

                                        GroupName3 = ""
                                        GroupName3 = CStr(result3.Properties("memberOf")(counter3))
                                        GroupArr3 = Split(GroupName3, ",")
                                        If Not (IsNothing(GroupArr3(0))) Then
                                            GroupName3 = Mid(GroupArr3(0), 4, Len(GroupArr3(0)) - 3)

                                            'set a new empty row
                                            Dim drGroup3 As DataRow = dtGroup.NewRow()

                                            'populate the column
                                            drGroup2("Gruppenname") = GroupName3 & " --> " & GroupName2 & " --> " & GroupName1
                                            drGroup2("Gruppentyp") = getGroupType(GroupName1)

                                            'append the row to the table of the dataset
                                            dtGroup.Rows.Add(drGroup3)
                                        End If
                                    Next

                                End If
                            Next

                        End If
                    Next
                End If
                ' Get primary Group
                PrimaryGroup = GetPrimaryGroupName(UserName)
                If PrimaryGroup.Length > 0 Then

                    'set a new empty row
                    Dim drGroup3 As DataRow = dtGroup.NewRow()

                    'populate the column
                    drGroup3("Gruppenname") = PrimaryGroup
                    drGroup3("Gruppentyp") = getGroupType(PrimaryGroup)

                    'append the row to the table of the dataset
                    dtGroup.Rows.Add(drGroup3)

                End If

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, " Error", MessageBoxButtons.OK, MessageBoxIcon.Error) 'This will give a description of the error.
            Return Nothing
            Exit Function
        Finally
            search1.Dispose()
            search2.Dispose()
            search3.Dispose()
            search4.Dispose()
            search5.Dispose()
        End Try
        Return dsGroup
    End Function
 
Back
Top