Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. <# This form was created using POSHGUI.com a free online gui designer for PowerShell
  2. .NAME
  3. Untitled
  4. #>
  5.  
  6. Add-Type -AssemblyName System.Windows.Forms
  7. [System.Windows.Forms.Application]::EnableVisualStyles()
  8.  
  9. $Form = New-Object system.Windows.Forms.Form
  10. $Form.ClientSize = '400,400'
  11. $Form.text = "Leaver Automation Tool"
  12. $Form.TopMost = $false
  13.  
  14. $Form.KeyPreview = $True
  15. $Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
  16. {$TextBox1.Text;$Button1.Click}})
  17. $Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
  18. {$Form.Close()}})
  19.  
  20.  
  21. $TextBox1 = New-Object system.Windows.Forms.TextBox
  22. $TextBox1.multiline = $false
  23. $TextBox1.width = 100
  24. $TextBox1.height = 20
  25. $TextBox1.location = New-Object System.Drawing.Point(47,26)
  26. $TextBox1.Font = 'Microsoft Sans Serif,10'
  27. $Form.Controls.Add($TextBox1)
  28.  
  29. $Reference = New-Object system.Windows.Forms.TextBox
  30. $Reference.multiline = $false
  31. $Reference.width = 100
  32. $Reference.height = 20
  33. $Reference.location = New-Object System.Drawing.Point(284,27)
  34. $Reference.Font = 'Microsoft Sans Serif,10'
  35.  
  36.  
  37. $Button1 = New-Object system.Windows.Forms.Button
  38. $Button1.text = "Get Name"
  39. $Button1.width = 80
  40. $Button1.height = 40
  41. $Button1.location = New-Object System.Drawing.Point(184,17)
  42. $Button1.Font = 'Microsoft Sans Serif,10'
  43. $Button1.Add_Click({
  44. $Nameofuser.Text = GetUsername})
  45. $Form.controls.Add($Button1)
  46.  
  47.  
  48. $Nameofuser = New-Object system.Windows.Forms.TextBox
  49. $Nameofuser.multiline = $false
  50. $Nameofuser.width = 328
  51. $Nameofuser.height = 20
  52. $Nameofuser.location = New-Object System.Drawing.Point(32,160)
  53. $Nameofuser.Font = 'Microsoft Sans Serif,10'
  54.  
  55. $DoLeaver = New-Object system.Windows.Forms.Button
  56. $DoLeaver.text = "Process Leaver"
  57. $DoLeaver.width = 224
  58. $DoLeaver.height = 127
  59. $DoLeaver.location = New-Object System.Drawing.Point(81,222)
  60. $DoLeaver.Font = 'Microsoft Sans Serif,16,style=Bold'
  61. $DoLeaver.Add_Click({
  62. ProcessLeaver;$Form.Close()})
  63. $Form.controls.Add($DoLeaver)
  64.  
  65. $ULabel = New-Object system.Windows.Forms.Label
  66. $ULabel.text = "Username"
  67. $ULabel.AutoSize = $true
  68. $ULabel.width = 25
  69. $ULabel.height = 10
  70. $ULabel.location = New-Object System.Drawing.Point(55,7)
  71. $ULabel.Font = 'Microsoft Sans Serif,10'
  72.  
  73. $CLabel = New-Object system.Windows.Forms.Label
  74. $CLabel.text = "Leaver Call Reference"
  75. $CLabel.AutoSize = $true
  76. $CLabel.width = 25
  77. $CLabel.height = 10
  78. $CLabel.location = New-Object System.Drawing.Point(273,7)
  79. $CLabel.Font = 'Microsoft Sans Serif,10'
  80.  
  81.  
  82. $Form.controls.AddRange(@($TextBox1,$Button1,$Nameofuser,$DoLeaver,$Reference,$ULabel,$CLabel))
  83.  
  84.  
  85.  
  86. #Write your logic code here
  87.  
  88.  
  89. Function GetUsername
  90. {
  91.  
  92. Get-ADUser -Identity $TextBox1.Text -Properties Name | Select Name
  93. }
  94.  
  95. Function ProcessLeaver{
  96. # Function to generate random password and assign to $NewPassword
  97. Function Random-Password ($length = 12)
  98. {
  99. $punc = 46..46
  100. $digits = 48..57
  101. $letters = 65..90 + 97..122
  102. $password = get-random -count $length `
  103. -input ($punc + $digits + $letters) |
  104. % -begin { $aa = $null } `
  105. -process {$aa += [char]$_} `
  106. -end {$aa}
  107. return $password
  108. }
  109. $NewPassword = Random-Password
  110.  
  111.  
  112. # Reset password
  113. $CurrentUser = Get-ADUser $TextBox1.Text -Properties distinguishedName,HomeDirectory, PrimaryGroup, MemberOf
  114. Set-ADAccountPassword -Identity $CurrentUser -NewPassword (ConvertTo-SecureString $NewPassword -AsPlainText -force) -Reset #reset the password
  115.  
  116. # Add to disabled accounts group
  117. Add-ADGroupMember -Identity "Disabled Accounts" -Members $Textbox1.Text #add the disabled accounts group to the account
  118.  
  119. # Add description and set primarygroupID
  120. Set-ADUser $Textbox1.Text -Description ("Leaver - " + $Reference.Text) -Enabled $false -Replace @{primaryGroupID=6431} #change description, disable account, set disabled accounts as primary
  121.  
  122. # Remove from all groups
  123. $Groups = $CurrentUser | Select -Expand MemberOf
  124. foreach ($Group in $Groups){
  125. $User = $Textbox1.Text
  126. $Ref = $Reference.Text
  127. $DateTime = (Get-Date -format "yyyyMMdd-HHmm")
  128. $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
  129.  
  130. Remove-ADGroupMember $Group -member $Textbox1.Text -Confirm:$false #TESTED ON AWS TEST ENVIRONMENT; works fine
  131.  
  132. }
  133.  
  134.  
  135. # Move to leavers OU
  136. Move-ADObject $CurrentUser.distinguishedName -TargetPath "OU=Leavers,DC=Kennedyslaw,DC=local" #move to leavers OU
  137.  
  138. # Rename Home driver folder to 'Leaver_'
  139. $LeaverDrive = $CurrentUser.HomeDirectory #name leaver drive variable using homedir attribute
  140. Rename-Item -Path $LeaverDrive -NewName $("Leaver_" + $Textbox1.Text) #rename the H drive to Leaver_Username
  141.  
  142. }
  143.  
  144. [void]$Form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement