Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <# This form was created using POSHGUI.com a free online gui designer for PowerShell
- .NAME
- Untitled
- #>
- Add-Type -AssemblyName System.Windows.Forms
- [System.Windows.Forms.Application]::EnableVisualStyles()
- $Form = New-Object system.Windows.Forms.Form
- $Form.ClientSize = '400,400'
- $Form.text = "Leaver Automation Tool"
- $Form.TopMost = $false
- $Form.KeyPreview = $True
- $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
- {$TextBox1.Text;$Button1.Click}})
- $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
- {$Form.Close()}})
- $TextBox1 = New-Object system.Windows.Forms.TextBox
- $TextBox1.multiline = $false
- $TextBox1.width = 100
- $TextBox1.height = 20
- $TextBox1.location = New-Object System.Drawing.Point(47,26)
- $TextBox1.Font = 'Microsoft Sans Serif,10'
- $Form.Controls.Add($TextBox1)
- $Reference = New-Object system.Windows.Forms.TextBox
- $Reference.multiline = $false
- $Reference.width = 100
- $Reference.height = 20
- $Reference.location = New-Object System.Drawing.Point(284,27)
- $Reference.Font = 'Microsoft Sans Serif,10'
- $Button1 = New-Object system.Windows.Forms.Button
- $Button1.text = "Get Name"
- $Button1.width = 80
- $Button1.height = 40
- $Button1.location = New-Object System.Drawing.Point(184,17)
- $Button1.Font = 'Microsoft Sans Serif,10'
- $Button1.Add_Click({
- $Nameofuser.Text = GetUsername})
- $Form.controls.Add($Button1)
- $Nameofuser = New-Object system.Windows.Forms.TextBox
- $Nameofuser.multiline = $false
- $Nameofuser.width = 328
- $Nameofuser.height = 20
- $Nameofuser.location = New-Object System.Drawing.Point(32,160)
- $Nameofuser.Font = 'Microsoft Sans Serif,10'
- $DoLeaver = New-Object system.Windows.Forms.Button
- $DoLeaver.text = "Process Leaver"
- $DoLeaver.width = 224
- $DoLeaver.height = 127
- $DoLeaver.location = New-Object System.Drawing.Point(81,222)
- $DoLeaver.Font = 'Microsoft Sans Serif,16,style=Bold'
- $DoLeaver.Add_Click({
- ProcessLeaver;$Form.Close()})
- $Form.controls.Add($DoLeaver)
- $ULabel = New-Object system.Windows.Forms.Label
- $ULabel.text = "Username"
- $ULabel.AutoSize = $true
- $ULabel.width = 25
- $ULabel.height = 10
- $ULabel.location = New-Object System.Drawing.Point(55,7)
- $ULabel.Font = 'Microsoft Sans Serif,10'
- $CLabel = New-Object system.Windows.Forms.Label
- $CLabel.text = "Leaver Call Reference"
- $CLabel.AutoSize = $true
- $CLabel.width = 25
- $CLabel.height = 10
- $CLabel.location = New-Object System.Drawing.Point(273,7)
- $CLabel.Font = 'Microsoft Sans Serif,10'
- $Form.controls.AddRange(@($TextBox1,$Button1,$Nameofuser,$DoLeaver,$Reference,$ULabel,$CLabel))
- #Write your logic code here
- Function GetUsername
- {
- Get-ADUser -Identity $TextBox1.Text -Properties Name | Select Name
- }
- Function ProcessLeaver{
- # Function to generate random password and assign to $NewPassword
- Function Random-Password ($length = 12)
- {
- $punc = 46..46
- $digits = 48..57
- $letters = 65..90 + 97..122
- $password = get-random -count $length `
- -input ($punc + $digits + $letters) |
- % -begin { $aa = $null } `
- -process {$aa += [char]$_} `
- -end {$aa}
- return $password
- }
- $NewPassword = Random-Password
- # Reset password
- $CurrentUser = Get-ADUser $TextBox1.Text -Properties distinguishedName,HomeDirectory, PrimaryGroup, MemberOf
- Set-ADAccountPassword -Identity $CurrentUser -NewPassword (ConvertTo-SecureString $NewPassword -AsPlainText -force) -Reset #reset the password
- # Add to disabled accounts group
- Add-ADGroupMember -Identity "Disabled Accounts" -Members $Textbox1.Text #add the disabled accounts group to the account
- # Add description and set primarygroupID
- Set-ADUser $Textbox1.Text -Description ("Leaver - " + $Reference.Text) -Enabled $false -Replace @{primaryGroupID=6431} #change description, disable account, set disabled accounts as primary
- # Remove from all groups
- $Groups = $CurrentUser | Select -Expand MemberOf
- foreach ($Group in $Groups){
- $User = $Textbox1.Text
- $Ref = $Reference.Text
- $DateTime = (Get-Date -format "yyyyMMdd-HHmm")
- $DateTime + " - Removing " + $TextBox1.Text + " from " + $Group | Out-File "\\Ukdcmsfsp02v\it\IT Teams\IT Helpdesk\Knowledge Bank\Joiners and Leavers\LEAVERS\Leaver Script Logs\$User-$Ref-$DateTime.log" -Append
- Remove-ADGroupMember $Group -member $Textbox1.Text -Confirm:$false #TESTED ON AWS TEST ENVIRONMENT; works fine
- }
- # Move to leavers OU
- Move-ADObject $CurrentUser.distinguishedName -TargetPath "OU=Leavers,DC=Kennedyslaw,DC=local" #move to leavers OU
- # Rename Home driver folder to 'Leaver_'
- $LeaverDrive = $CurrentUser.HomeDirectory #name leaver drive variable using homedir attribute
- Rename-Item -Path $LeaverDrive -NewName $("Leaver_" + $Textbox1.Text) #rename the H drive to Leaver_Username
- }
- [void]$Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement