Disabling Accounts from AD

rjcrane

New member
Joined
Dec 9, 2013
Messages
1
Programming Experience
Beginner
I am trying to create a windows form application/tool that speeds up the process of the way we disable employees account for leave our organization. So far I have created a form that allows the user to input the user name of the account and click a button that searches active directory and displays who the account belongs two, the next step that I am trying to implement is after clicking a second button the application would disable the account, move to the disabled users OU, update the description and hide from the global address list, however I am having issue link the returned account so that it can move on to disable the account, this is where your help would be appreciated in the best way forward. Thanks

Imports System.DirectoryServices.AccountManagement
Imports System.DirectoryServices
Public Class FrmLeaversTool
    Dim insPrincipalContext As New PrincipalContext(ContextType.Domain, "domain", "DC=,DC=,DC=com")
    Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
    Private Sub ListUsers(ByVal strUsername As String)
        Dim insUserPrincipal As New UserPrincipal(insPrincipalContext)
        insUserPrincipal.SamAccountName = strUsername
        SearchUsers(insUserPrincipal)
    End Sub
    Private Sub SearchUsers(ByVal parUserPrincipal As UserPrincipal)
        Dim insPrincipalSearcher As New PrincipalSearcher()
        'Dim currentADUser As System.DirectoryServices.AccountManagement.UserPrincipal
        insPrincipalSearcher.QueryFilter = parUserPrincipal
        Dim results As PrincipalSearchResult(Of Principal) = insPrincipalSearcher.FindAll
        For Each p As Principal In results
            currentADUser = p
            If currentADUser.Enabled = True Then
                LogDetails("Account Name: " & currentADUser.GivenName & " " & currentADUser.Surname)
                LogDetails("Account Enabled: " & currentADUser.Enabled)
                LogDetails("Account Description: " & currentADUser.Description)
                LogDetails("Account Password Set on: " & currentADUser.LastPasswordSet)
            Else
                MsgBox("Account already disabled")
            End If
        Next
    End Sub
    Private Sub LogDetails(ByVal strString As String)
        txtResults.Text = txtResults.Text & strString & vbCrLf
    End Sub
    Private Sub cmdFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFind.Click
        ListUsers(txtUsername.Text)
    End Sub
    Private Sub cmdRun_Click(sender As Object, e As EventArgs) Handles cmdRun.Click
        tmrProgress.Start()
        lblStatus.Text = "In Progress"
    End Sub
    Private Sub tmrProgress_Tick(sender As Object, e As EventArgs) Handles tmrProgress.Tick
        pgbProgress.Increment(1)
            cmdReset.Enabled = False
            Me.Cursor = Cursors.WaitCursor
            If pgbProgress.Value = 10 Then
            chk1.Checked = True
            'Disable Account
            End If
            If pgbProgress.Value = 30 Then
            chk2.Checked = True
            'Reset password
            End If
            If pgbProgress.Value = 50 Then
            chk3.Checked = True
            'Description updated
            End If
            If pgbProgress.Value = 70 Then
            chk4.Checked = True
            'Hidden from the GAL
            End If
            If pgbProgress.Value = 90 Then
            chk5.Checked = True
            'Move account to disabled OU
            End If
            If pgbProgress.Value = pgbProgress.Maximum Then
                lblStatus.Text = "Completed"
                tmrProgress.Stop()
                Me.Cursor = Cursors.Default
                pgbProgress.Value = 0
                cmdSend.Enabled = True
                cmdReset.Enabled = True
            End If
    End Sub
 
Top Bottom