Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

VB .NET add AD user to group

Avatar of EfrenM
EfrenMFlag for United States of America asked on
Visual Basic ClassicVisual Basic.NETVB Script
6 Comments1 Solution418 ViewsLast Modified:
Hey Guys

im working on a piece of the code that adds a user to some groups it works fine if i hard code it
Dim arrGroups As String() = G test1", "GTEST2", "GTEST3"}

what i would like to do is add the groups from a textbox i have , the richtext box looks like this
GTEST1
GTEST2
GTEST3


Const ADPath As String = "LDAP://DC=ABC,DC=local"
        Dim userPath As String = "LDAP://" & NewUser.Properties("distinguishedname").Value() '& "," & ADPath.Substring(ADPath.LastIndexOf("/") + 1)
        Dim arrGroups As String() = G test1", "GTEST2", "GTEST3"}

     
        For Each Group As String In arrGroups
            AddUserToGroup2(userPath, Group)
        Next
end sub


Sub AddUserToGroup2(ByVal usr As String, ByVal grp As String)
        Dim adpath As String
        Using de As New DirectoryEntry(adpath)
            Using ds As New DirectorySearcher(de, "(CN=" & grp & ")")
                Dim sr As SearchResult = ds.FindOne
                Dim deGroup As DirectoryEntry = New DirectoryEntry(sr.Path)
                Dim deUser As DirectoryEntry = New DirectoryEntry(usr)
                If Convert.ToBoolean(deGroup.Invoke("IsMember", New Object() {deUser.Path})) = False Then
                    Try
                        deGroup.Invoke("Add", New Object() {deUser.Path})
                    Catch ex As Exception
                        ' MessageBox.Show(ex.Message)
                    End Try
                End If
            End Using
        End Using
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of Zhaolai
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answers