Question Remove user from AD ?

sebasjuh

Member
Joined
Dec 6, 2007
Messages
13
Programming Experience
Beginner
Hello,

I have a small question!
Is it possible to remove a user from a Active Directory group within Visual Basic 2005?

I'm making a tool to get all the groups and users, so I already have to group name and the username. Now I only want a button to remove the selected user from the selected group but how can I get that?

Can someone help me with this maybe???

Sebas
 
Thank you MattP for your reply.
I already found that website but when I use that code for deleting a user from a group I got this error:

Name 'GetDirectoryObject' is not declared.

I don't know how to fix this....


Edit: This is the code btw:
VB.NET:
Public Shared Sub RemoveUserFromGroup(ByVal UserName As String, _
                  ByVal GroupName As String)

    Dim Domain As New String("")
    
    'get reference to group
    Domain = "/CN=" + GroupName + ",CN=Users," + GetLDAPDomain()
    Dim oGroup As DirectoryEntry = GetDirectoryObject(Domain)
    
    'get reference to user
    Domain = "/CN=" + UserName + ",CN=Users," + GetLDAPDomain()
    Dim oUser As DirectoryEntry = GetDirectoryObject(Domain)
    
    'Add the user to the group via the invoke method
    oGroup.Invoke("Remove", New Object() {oUser.Path.ToString()})
    
    oGroup.Close()
    oUser.Close()
End Sub
 
Last edited:
No problem

If you have allready connected to the user using directoryentry, you can get the distinguished name of the user.

dim userdn as string = %userDE%.properties("distinguishedname").value

or by removing the "LDAP://" from the users path.
dim userdn as string = %userDE%.path.remove(0,7)


then you have to open the group using directoryentry

dim groupde as new directoryentry(%your LDAP to the group%)
groupde.properties("member").remove(userdn)
groupde.commitchanges
groupde.close
 
Back
Top