Advertisement
Guest User

AD Reset/Unlock

a guest
Mar 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $formBHSADAccountPassword_Load={
  2.     #TODO: Initialize Form Controls here
  3.    
  4. }
  5. #region Variables for Warning Box
  6. $MessageBoxTitle = "Warning: AD Module Missing"
  7. $Messageboxbody = "The Active Directory Powershell Module is not installed.  Please install in order to use this tool."
  8. $MessageIcon = "Warning"
  9. $ADMod = Get-Module -ListAvailable -Name ActiveDirectory
  10. #endregion
  11.  
  12. #region Test for AD Powershell Module install
  13. if (-not ($ADMod))
  14. {
  15.     [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $messageicon)
  16. }
  17. else
  18. {
  19. Import-Module ActiveDirectory
  20. }
  21. #endregion
  22.  
  23. #region Button actions
  24. $buttonLookup_Click={
  25.     $OutputBox1.Text = Do-Lookup -Username "$($LookupField1.Text)" |
  26.     Format-List @{ Label = "Full Name"; Expression = { ($_.Name) } }, @{ Label = "Username"; Expression = { ($_.SamAccountName) } }, @{ Label = "Dept"; Expression = { ($_.Department) } }, @{ Label = "Locked Out"; Expression = { ($_.LockedOut) } } |
  27.     Out-String
  28. }
  29.  
  30. $buttonUnlockReset_Click = {
  31.     if ($unlock.checked -eq $true)
  32.     {
  33.         $Outputbox1.Text = Do-Unlock -Username "$($UsernameField.Text)"
  34.     }
  35.     else
  36.     {
  37.         $Outputbox1.Text = Do-Reset -Username "$($UsernameField.Text)"
  38.     }
  39. }
  40. #endregion
  41.  
  42. #region Lookup Function
  43. function Do-Lookup
  44. {
  45.     Param(
  46.     $Username
  47.     )
  48.     Get-ADUser -Filter * -Properties SamAccountName, Department, LockedOut | where { $_.Name -like "*$Username*" } | select SamAccountName, Name, Department, LockedOut
  49.    
  50. }
  51. #endregion
  52.  
  53. #region Unlock and reset password function
  54. function Do-Reset
  55. {
  56.     Param (
  57.     $Username
  58.     )
  59.     Unlock-ADAccount -Identity "$Username"
  60.     Set-ADAccountPassword "$Username" -Newpassword (ConvertTo-SecureString Bournewood1 -AsPlainText -Force) -Reset -PassThru | Set-ADuser -ChangePasswordAtLogon $True
  61. }
  62. #endregion
  63.  
  64. #region Unlock only function
  65. function Do-Unlock
  66. {
  67.     Param (
  68.         $Username
  69.     )
  70.     Unlock-ADAccount -Identity "$Username"
  71. }
  72. #endregion
  73.  
  74. #region Don't know why these are here, PowerShell Studio bitches when I remove them.
  75. $UsernameField_TextChanged={
  76.     #TODO: Place custom script here
  77.    
  78. }
  79.  
  80.  
  81. $LookupField1_TextChanged={
  82.     #TODO: Place custom script here
  83.    
  84. }
  85. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement