Imports System.DirectoryServices
Public Class WindowsGroups
Inherits System.Windows.Forms.Form
Private MyDomain As String
Private MyServer As String
Private MyGroup As String
Private MyTimerCount As Integer
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents cbUsers As System.Windows.Forms.ComboBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtGroupName As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtDomain As System.Windows.Forms.TextBox
Friend WithEvents Panel2 As System.Windows.Forms.Panel
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtServer As System.Windows.Forms.TextBox
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents PanelUsers As System.Windows.Forms.Panel
Friend WithEvents lblGroup As System.Windows.Forms.Label
Friend WithEvents Panel1 As System.Windows.Forms.Panel
Friend WithEvents Timer1 As System.Windows.Forms.Timer
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.cbUsers = New System.Windows.Forms.ComboBox
Me.Label1 = New System.Windows.Forms.Label
Me.txtGroupName = New System.Windows.Forms.TextBox
Me.PanelUsers = New System.Windows.Forms.Panel
Me.lblGroup = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Button2 = New System.Windows.Forms.Button
Me.Button1 = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.txtDomain = New System.Windows.Forms.TextBox
Me.Panel2 = New System.Windows.Forms.Panel
Me.Button4 = New System.Windows.Forms.Button
Me.txtServer = New System.Windows.Forms.TextBox
Me.Label4 = New System.Windows.Forms.Label
Me.Panel1 = New System.Windows.Forms.Panel
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.PanelUsers.SuspendLayout()
Me.Panel2.SuspendLayout()
Me.Panel1.SuspendLayout()
Me.SuspendLayout()
'
'cbUsers
'
Me.cbUsers.Location = New System.Drawing.Point(8, 24)
Me.cbUsers.Name = "cbUsers"
Me.cbUsers.Size = New System.Drawing.Size(256, 21)
Me.cbUsers.TabIndex = 4
'
'Label1
'
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(208, 16)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Select User"
'
'txtGroupName
'
Me.txtGroupName.Location = New System.Drawing.Point(8, 120)
Me.txtGroupName.Name = "txtGroupName"
Me.txtGroupName.Size = New System.Drawing.Size(256, 20)
Me.txtGroupName.TabIndex = 2
Me.txtGroupName.Text = "Enter Group Name"
'
'PanelUsers
'
Me.PanelUsers.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PanelUsers.Controls.Add(Me.lblGroup)
Me.PanelUsers.Controls.Add(Me.ListBox1)
Me.PanelUsers.Location = New System.Drawing.Point(416, 8)
Me.PanelUsers.Name = "PanelUsers"
Me.PanelUsers.Size = New System.Drawing.Size(240, 312)
Me.PanelUsers.TabIndex = 3
Me.PanelUsers.Visible = False
'
'lblGroup
'
Me.lblGroup.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblGroup.Location = New System.Drawing.Point(8, 8)
Me.lblGroup.Name = "lblGroup"
Me.lblGroup.Size = New System.Drawing.Size(208, 16)
Me.lblGroup.TabIndex = 9
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(8, 24)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(224, 277)
Me.ListBox1.TabIndex = 8
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(96, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 6
Me.Button2.Text = "Remove"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 5
Me.Button1.Text = "Add"
'
'Label2
'
Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.Location = New System.Drawing.Point(8, 104)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(208, 16)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Windows Group"
'
'Label3
'
Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.Location = New System.Drawing.Point(8, 8)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(208, 16)
Me.Label3.TabIndex = 5
Me.Label3.Text = "Windows Domain Name"
'
'txtDomain
'
Me.txtDomain.Location = New System.Drawing.Point(8, 24)
Me.txtDomain.Name = "txtDomain"
Me.txtDomain.Size = New System.Drawing.Size(256, 20)
Me.txtDomain.TabIndex = 0
Me.txtDomain.Text = "Enter Domain Name"
'
'Panel2
'
Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Panel2.Controls.Add(Me.Button4)
Me.Panel2.Controls.Add(Me.txtServer)
Me.Panel2.Controls.Add(Me.Label4)
Me.Panel2.Controls.Add(Me.Label3)
Me.Panel2.Controls.Add(Me.txtDomain)
Me.Panel2.Controls.Add(Me.Label2)
Me.Panel2.Controls.Add(Me.txtGroupName)
Me.Panel2.Location = New System.Drawing.Point(8, 8)
Me.Panel2.Name = "Panel2"
Me.Panel2.Size = New System.Drawing.Size(392, 184)
Me.Panel2.TabIndex = 6
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(8, 152)
Me.Button4.Name = "Button4"
Me.Button4.TabIndex = 3
Me.Button4.Text = "Go"
'
'txtServer
'
Me.txtServer.Location = New System.Drawing.Point(8, 72)
Me.txtServer.Name = "txtServer"
Me.txtServer.Size = New System.Drawing.Size(256, 20)
Me.txtServer.TabIndex = 1
Me.txtServer.Text = "Enter Server Name"
'
'Label4
'
Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.Location = New System.Drawing.Point(8, 54)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(208, 16)
Me.Label4.TabIndex = 6
Me.Label4.Text = "Domain Controller"
'
'Panel1
'
Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Panel1.Controls.Add(Me.Label1)
Me.Panel1.Controls.Add(Me.cbUsers)
Me.Panel1.Controls.Add(Me.Button2)
Me.Panel1.Controls.Add(Me.Button1)
Me.Panel1.Location = New System.Drawing.Point(8, 216)
Me.Panel1.Name = "Panel1"
Me.Panel1.Size = New System.Drawing.Size(392, 104)
Me.Panel1.TabIndex = 7
Me.Panel1.Visible = False
'
'Timer1
'
Me.Timer1.Interval = 100000
'
'WindowsGroups
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(664, 326)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.PanelUsers)
Me.Name = "WindowsGroups"
Me.Text = "Group ManageMent"
Me.PanelUsers.ResumeLayout(False)
Me.Panel2.ResumeLayout(False)
Me.Panel1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Load_Group()
Me.ListBox1.Items.Clear()
'This will list all users in the Domain
Dim strDirEntryPath As String
strDirEntryPath = "WinNT://" & MyDomain & "/" & MyServer & "/" & MyGroup & ",group"
Dim group As New DirectoryEntry(strDirEntryPath)
Dim users As Object
users = group.Invoke("members")
Dim user1 As Object
Dim DT As New DataTable
Dim DC As New DataColumn("users")
'DC.DataType = System.Type.GetType("string")
DT.Columns.Add(DC)
Dim r1 As DataRow
Dim StrName As String
'I used a data table simply to sort
'Probably a better way, but since I use data sets all the time that's what I know
For Each user1 In CType(users, IEnumerable)
r1 = DT.NewRow
Dim userEntry As New System.DirectoryServices.DirectoryEntry(user1)
r1.Item(0) = userEntry.Name
DT.Rows.Add(r1)
Next
'Use data view to sort
Dim DV As New DataView(DT)
DV.Sort = "users asc"
Dim intCount As Integer
intCount = 0
'Add Items to List Box
For intCount = 0 To DV.Count - 1
Me.ListBox1.Items.Add(DV.Item(intCount).Row.Item(0))
Next
End Sub
Private Sub Load_All_Users()
'This will list all users in the Domain
Dim strDirEntryPath As String
strDirEntryPath = "WinNT://" & MyDomain & "/" & MyServer & "/" & "Domain Users" & ",group"
Dim group As New DirectoryEntry(strDirEntryPath)
Dim users As Object
users = group.Invoke("members")
Dim user1 As Object
'I used a data table simply to sort
'Probably a better way, but since I use data sets all the time that's what I know
Dim DT As New DataTable
Dim DC As New DataColumn("users")
DT.Columns.Add(DC)
Dim r1 As DataRow
Dim StrName As String
'Add each user to the data table
For Each user1 In CType(users, IEnumerable)
r1 = DT.NewRow
Dim userEntry As New System.DirectoryServices.DirectoryEntry(user1)
r1.Item(0) = userEntry.Name
DT.Rows.Add(r1)
Next
'Use Data View to Sort Articles
Dim DV As New DataView(DT)
DV.Sort = "users asc"
Dim intCount As Integer
intCount = 0
For intCount = 0 To DV.Count - 1
Me.cbUsers.Items.Add(DV.Item(intCount).Row.Item(0))
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Nothing happens on the form load
End Sub
Private Sub txtGroupName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtGroupName.Enter
'Clear Text Box when cursor enters
Me.txtGroupName.Text = ""
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'AD_Group object is created
'Pass parameters
Dim Obj As New AD_Group(MyDomain, MyServer, MyGroup)
Try
Obj.AddUser(Me.cbUsers.SelectedItem)
Catch ex As Exception
'Trap Error
MsgBox("An Error Occured, Message = " & ex.Message)
'Show inner exception
'The inner exception will let the user know if
'user is already belongs to the group
If Not ex.InnerException Is Nothing Then
MsgBox("Inner Exception = " & ex.InnerException.Message)
End If
Exit Sub
End Try
Me.ListBox1.Items.Clear()
Load_Group()
MsgBox(Me.cbUsers.SelectedItem & " was added to " & MyGroup)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Obj As New AD_Group(MyDomain, MyServer, MyGroup)
Try
Obj.RemoveUser(Me.cbUsers.SelectedItem)
Catch ex As Exception
MsgBox("An Error Occured, Message = " & ex.Message)
'Show inner exception
'The inner exception will let the user know if the
'user already doesn't belong to the group
If Not ex.InnerException Is Nothing Then
MsgBox("Inner Exception = " & ex.InnerException.Message)
End If
Exit Sub
End Try
Me.ListBox1.Items.Clear()
Load_Group()
MsgBox(Me.cbUsers.SelectedItem & " was removed from " & MyGroup)
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Validate that the user has entered Domain, Server (DC) and Group
If Me.txtDomain.Text = "" Or Me.txtDomain.Text = "Enter Domain Name" Then
MsgBox("Enter a Domain name")
Me.txtDomain.Focus()
Exit Sub
End If
If Me.txtServer.Text = "" Or Me.txtServer.Text = "Enter Server Name" Then
MsgBox("Enter a Domain Controller name")
Me.txtDomain.Focus()
Exit Sub
End If
If Me.txtGroupName.Text = "" Or Me.txtGroupName.Text = "Enter Group Name" Then
MsgBox("Enter a group name")
Me.txtGroupName.Focus()
Exit Sub
End If
MyDomain = Trim(Me.txtDomain.Text)
MyServer = Trim(Me.txtServer.Text)
MyGroup = Trim(Me.txtGroupName.Text)
Try 'Load users
Load_All_Users()
Catch ex As Exception
MsgBox("An Error occured while attempting to load all users", MsgBoxStyle.Critical)
MsgBox("Message = " & ex.Message)
'Exception returned from com object
If Not ex.InnerException Is Nothing Then
MsgBox("Inner Exception = " & ex.InnerException.Message)
End If
Me.PanelUsers.Visible = False
Me.Panel1.Visible = False
Exit Sub
End Try
Try 'Load Group
Load_Group()
Catch ex As Exception
MsgBox("An Error occured while attempting to load the Group", MsgBoxStyle.Critical)
MsgBox("Message = " & ex.Message)
If Not ex.InnerException Is Nothing Then
MsgBox("Inner Exception = " & ex.InnerException.Message)
End If
Me.PanelUsers.Visible = False
Me.Panel1.Visible = False
Exit Sub
End Try
' Dim Group As New AD_Group(MyDomain, MyServer, MyGroup)
'The Timer is used to refresh the list of users in the group.
'My office has more then one domain controller so the list isn't updated automatically
'The timer runs every ten seconds to update the listbox
Me.Timer1.Enabled = True
Me.lblGroup.Text = "Members of " & Me.txtDomain.Text & "\" & Me.txtGroupName.Text
Me.PanelUsers.Visible = True
Me.Panel1.Visible = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'The time is to update the listbox every 10 seconds since it's not instant.
Try
Me.ListBox1.Items.Clear()
Load_Group()
Catch ex As Exception
MsgBox("Refresh of List Box with Group Memebers Failed. Message = " & ex.Message, MsgBoxStyle.Critical)
'Display com exception
If Not ex.InnerException Is Nothing Then
MsgBox("Inner Exception = " & ex.InnerException.Message)
End If
End Try
End Sub
End Class