Guest User

Powershell Unlock Active Directory Account

a guest
Apr 8th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Purpose:         Unlocks Active Directory accounts
  2. # Requirements:    Network admin rights
  3. # Author:          vocatus on reddit.com/r/usefulscripts
  4. # History:         1.0 Initial write
  5. # Usage:           Pass account names to be unlocked as arguments, e.g. .\unlock_AD_account.ps1 john.smith fry leela
  6.  
  7.  
  8. #############
  9. # VARIABLES # -- Set these
  10. #############
  11. # Logging information. No trailing slashes (\) on directory names
  12. $LOGPATH=$env:systemdrive + "\Logs"
  13. $LOGFILE=$env:computername + "_unlock_AD_account.log"
  14.  
  15. # Don't touch anything below this line
  16. $CUR_DATE=get-date -f "yyyy-MM-dd"
  17.  
  18. #############
  19. # EXECUTION #
  20. #############
  21. # If no arguments were passed, spit out a message and die.
  22. if (! $args) {
  23.     write-host
  24.     Write-Host "Pass names of accounts to unlock, separated by spaces. e.g. .\unlock_AD_account.ps1 MyAccountName MySecondAccountName" -foregroundcolor white
  25.     write-host
  26.     Break
  27.     }
  28.  
  29. # Log that the script was triggered
  30. "$CUR_DATE "+ $(get-date -f "hh:mm:ss") + " Account lockout script triggered. Executing..." >> $LOGPATH\$LOGFILE
  31.  
  32. # Do the unlock
  33. foreach ($i in $args) {
  34.     unlock-adaccount $i
  35.     write-host $i unlocked -foregroundcolor green
  36.     "$CUR_DATE "+ $(get-date -f "hh:mm:ss") + " $i unlocked" >> $LOGPATH\$LOGFILE
  37.     #if $LASTEXITCODE -ne "0" write-host $i failed to unlock -foregroundcolor red
  38.     }
Add Comment
Please, Sign In to add comment