directoryentry and directorysearcher to add users to group

wiredteknologies

New member
Joined
Oct 28, 2008
Messages
1
Programming Experience
Beginner
Working a VB app that creates a DL and adds a list of users to that DL. It creates the group properly and it finds the users. But I cannot get it to add the users to the group. I spent a good bit of my day doing research trying to work this out. But to no avail. Let me know if you see anything i should try.

with the code i get the following error message:

An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll

Additional information: Exception has been thrown by the target of an invocation.


VB.NET:
    Private Sub btnDLCreateAddUsr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDLCreateAddUsr.Click


        Dim i As Integer
        Dim str As String
        Dim custid As String
        Dim dlname As String = txtDLName.Text
        Const ADS_GROUP_TYPE_UNIVERSAL_GROUP = &H8
        Dim strDLOU As String = txtDLOU.Text
        Dim objOU As New System.DirectoryServices.DirectoryEntry(strDLOU)
        Dim objDL As DirectoryServices.DirectoryEntry = objOU.Children.Add("CN=" & dlname, "group")
        Dim entry As New DirectoryServices.DirectoryEntry("LDAP://DC=abc,DC=main,DC=company,DC=com")
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult


        objDL.Properties("grouptype").Value = ADS_GROUP_TYPE_UNIVERSAL_GROUP
        objDL.Properties("samAccountName").Value = dlname
        'objDL.CommitChanges()

        For i = 0 To lstSapID.Items.Count - 1
            custid = lstSapID.Items.Item(i)
            mySearcher.Filter = ("(&(objectclass=user)(extensionattribute4=" & custid & "))")
            If Len(sapid) <> 8 Then
                MsgBox(sapid & " is an invalid length for a SAP ID.")

            Else
                'MsgBox(sapid & "is a valid SAP ID.")
                For Each result In mySearcher.FindAll()
                    'MsgBox(result.Path)
                    'objDL.Properties("member").Add(result.Properties("distinguishedname")(0).ToString)
                    [B]objDL.Invoke("add", New Object() {result.Properties("distinguishedname")(0).ToString})[/B] 'line that generates the error
                    'objDL.add()
                Next



            End If

        Next

        objDL.CommitChanges()
        MsgBox(dlname & " Created successfully")


        'cleanup()
        dlname = Nothing
        strDLOU = Nothing
        objOU = Nothing
        objDL = Nothing

    End Sub
 
Last edited:
Back
Top