Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Heya All,

I am having trouble adding an already created user to a group.

Looking over the MS sites and it seems I have the syntax right put it is throwing up "Unknown Object" Errors.

VB
Sub AddUserToAGroup(ByVal GroupName As String, ByVal UserCN As String)

            Dim dirEntry As New DirectoryEntry
            dirEntry.Path = "LDAP://OU=Some_Site,DC=Domain,DC=NET"
            dirEntry.Username = sADUsername
            dirEntry.Password = sADPassword


            If UserExists(UserCN) = True Then

                Dim group As DirectoryEntry = dirEntry.Children.Find("CN=" & GroupName, "group")
                Dim usr As DirectoryEntry = group.Children.Find("CN=" & UserCN, "user")

                If group IsNot "" Then
                    group.Properties("member").Add(usr.Properties("distinguishedName").Value)
                    group.CommitChanges()
                End If
            ElseIf UserExists(UserCN) = False Then
                Exit Sub
            End If
        End Sub
Posted
Updated 12-Dec-11 19:50pm
v3

1 solution

May this link will help you link
 
Share this answer
 
Comments
Zac Newman 13-Dec-11 17:13pm    
That uses the Invoke("Add",new object function call, I was trying to avoid using that if I could. All of the MS sites suggest using the method displayed in my original code. I have tested with the invoke call and was having very littler luck.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900