-
Notifications
You must be signed in to change notification settings - Fork 79
/
AD_unlock_account.ps1
56 lines (46 loc) · 1.92 KB
/
AD_unlock_account.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<#
Purpose: Unlocks Active Directory accounts
Requirements: Network admin rights
Author: reddit.com/user/vocatus ( [email protected] ) // PGP key: 0x07d1490f82a211a2
History: 1.1.0 + Added Prep section with standard variables to be consistent with other scripts
1.0.0 Initial write
Usage: Pass account names to be unlocked as arguments, e.g. .\unlock_AD_account.ps1 MyAccountName MySecondAccountName
#>
#############
# VARIABLES # -- Set these to your desired values
#############
# Rules for variables:
# * Quotes are required (e.g.: "c:\directory\path" )
# * NO trailing slashes on paths! (bad: "c:\directory\" )
# * Spaces are okay (okay: "c:\my folder\with spaces" )
# * Network paths are okay (okay: "\\server\share name" )
# ( "\\172.16.1.5\share name" )
# Logging information
$LOGPATH=$env:systemdrive + "\Logs"
$LOGFILE=$env:computername + "_AD_unlock_account.log"
########
# Prep #
########
$SCRIPT_VERSION="1.1.0"
$SCRIPT_UPDATED="2014-01-16"
$CUR_DATE=get-date -f "yyyy-MM-dd"
#############
# EXECUTION #
#############
# If no arguments were passed, spit out a message and die.
# AKA if "$args" is false / aka not true, then do this stuff
if (! $args) {
write-host
Write-Host "Pass names of accounts to unlock, separated by spaces. e.g. .\unlock_AD_account.ps1 MyAccountName MySecondAccountName" -f white
write-host
Break
}
# Log that the script was triggered
"$CUR_DATE "+ $(get-date -f "hh:mm:ss") + " Account unlock script triggered. Executing..." >> $LOGPATH\$LOGFILE
# Do the unlock
foreach ($i in $args) {
unlock-adaccount $i
write-host $i unlocked -f green
"$CUR_DATE "+ $(get-date -f "hh:mm:ss") + " $i unlocked" >> $LOGPATH\$LOGFILE
#if $LASTEXITCODE -ne "0" write-host $i failed to unlock -f red
}