I'm using a snippet of code that can be found here
http://www.codeproje...ctory-in-VB-NET
It is a sub that is meant to add a user to a group in Active Directory. However, I can't seem to figure out how to use it. Here is the code:
''' <summary> ''' Method to add a user to a group ''' </summary> ''' <param name="de">DirectoryEntry to use</param> ''' <param name="deUser">DirectoryEntry (User) to use</param> ''' <param name="GroupName">Group name (in string formation) to search and ultimately add user to</param> Public Shared Sub AddUserToGroup(ByVal de As DirectoryEntry, ByVal deUser As DirectoryEntry, ByVal GroupName As String) Dim deSearch As DirectorySearcher = New DirectorySearcher() deSearch.SearchRoot = de deSearch.Filter = "(&(objectClass=group) (cn=" & GroupName & "))" Dim results As SearchResultCollection = deSearch.FindAll() Dim isGroupMember As Boolean = False If results.Count > 0 Then Dim group As New DirectoryEntry(results(0).Path) Dim members As Object = group.Invoke("Members", Nothing) For Each member As Object In CType(members, IEnumerable) Dim x As DirectoryEntry = New DirectoryEntry(member) Dim name As String = x.Name If name <> deUser.Name Then isGroupMember = False Else isGroupMember = True Exit For End If Next member If (Not isGroupMember) Then group.Invoke("Add", New Object() {deUser.Path.ToString()}) End If group.Close() End If Return End Sub
I'm not sure what to put for the first argument when calling it. For example:
AddUserToGroup(?, User, Group)
Thanks