|
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.