Trying to remove a user from multiple security groups using VB.NET

Posted on 2008-11-11
Last Modified: 2012-06-27
I'm trying to remove a user from multiple security groups using VB.NET. I can do it in vbs but .NET is killing me :o(
I know I have to find the user, enumerate what they have and then loop around each one removing them from the groups but I'm having no joy.
Looked around the web and can't quite put 2 and 2 together.

Would it be possible please for someone to throw together a brief bit of code that will:
1) Find one user in AD
2) List what groups they are in (if any)
3) Remove that user from those groups

I'm using Visual Studio 2008, got all my Imports ect and would appreciate this as I'm running out of time on a project.
Question by:stubar
    LVL 41

    Accepted Solution

    Okey's this for starters
    'add a Refernce to "System.DirectoryServices"
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim ldap As String
            Dim obj As Object
            Dim entry, grp As System.DirectoryServices.DirectoryEntry
            Dim srch As System.DirectoryServices.DirectorySearcher
            Dim result As System.DirectoryServices.SearchResult
            ' Step 1... open AD
            ldap = "LDAP://" & DomainName.Text
            entry = New System.DirectoryServices.DirectoryEntry(ldap)
            srch = New DirectoryServices.DirectorySearcher(entry)
            ' Step 2... perform the search
            srch.Filter = "(&(&(objectClass=user)(objectCategory=person))(Name=" & UserName.Text & "))"
            result = srch.FindOne()
            ' a quick sanity check
            If IsNothing(result) Then
                MsgBox("Yikes, can't find that user!")
                Exit Sub
            End If
            ' Step 3... get the list of groups
            For Each obj In CType(result.GetDirectoryEntry.Invoke("Groups"), System.Collections.IEnumerable)
                ' cast it back to DirectoryEntry
                grp = New System.DirectoryServices.DirectoryEntry(obj)
                ' Step 4... remove the user from the group
        End Sub
    End Class

    Open in new window


    Author Closing Comment

    Apologies for the tardiness.
    In the end I went with another option but as this solution fits the bill I am still awarding the points.
    Thanks again.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now