Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / Visual Basic

Updating Windows Security Groups using VB.NET and Directory Services

Rate me:
Please Sign up or sign in to vote.
4.09/5 (12 votes)
22 Dec 2004 65.9K   608   37  
How to use directory services to manage group level security. Easily Add, Remove and List users in a Windows domain security group. Note: You must be a Domain Admin to use this code.
Imports System.DirectoryServices
'This class uses the directory services dll to update Active Directory
'The user is added to the security group instantly
'IF you have 2 or more Domain Controllers there will be a lag or time lapse
'It might be One Minute or more Before the ReturnUsers() Function reflects changes to the AD





Public Class AD_Group

    Private DomainNameValue As String
    Private ServerNameValue As String
    Private GroupNameValue As String

    Public Sub New(ByVal DomainName As String, ByVal ServerName As String, ByVal GroupName As String)

        DomainNameValue = DomainName
        ServerNameValue = ServerName
        GroupNameValue = GroupName

    End Sub

    Public Function RemoveUser(ByRef UserName As String)
        Dim strDirEntryPath As String
        strDirEntryPath = "WinNT://" & DomainNameValue & "/" & ServerNameValue & "/" & GroupNameValue & ",group"

        Dim StrUserEntry As String
        StrUserEntry = "WinNT://" & DomainNameValue & "/" & ServerNameValue & "/" & UserName


        Dim group As New DirectoryEntry(strDirEntryPath)

        group.Invoke("Remove", New Object() {StrUserEntry})
        group.CommitChanges()

    End Function
    Public Function AddUser(ByRef UserName As String)
        Dim strDirEntryPath As String
        strDirEntryPath = "WinNT://" & DomainNameValue & "/" & ServerNameValue & "/" & GroupNameValue & ",group"

        Dim StrUserEntry As String
        StrUserEntry = "WinNT://" & DomainNameValue & "/" & ServerNameValue & "/" & UserName


        Dim group As New DirectoryEntry(strDirEntryPath)

        group.Invoke("Add", New Object() {StrUserEntry})
        group.CommitChanges()

    End Function
    Public Function ReturnUsers()
        Dim strDirEntryPath As String
        strDirEntryPath = "WinNT://" & DomainNameValue & "/" & ServerNameValue & "/" & GroupNameValue & ",group"
        Dim users As Object

        Dim group As New DirectoryEntry(strDirEntryPath)
        users = group.Invoke("members")

        Dim user1 As Object
        Dim UsersCollection As New Collection

        For Each user1 In CType(users, IEnumerable)
            Try
                Dim userEntry As New System.DirectoryServices.DirectoryEntry(user1)
                UsersCollection.Add(userEntry.Name)
            Catch e1 As Exception
                Return e1
                Exit Function
            End Try
        Next

        Return UsersCollection
    End Function


End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Todd is currently working at PA Lumbermens as a .net programmer.

Comments and Discussions