-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADUnlockLoop.ps1
112 lines (101 loc) · 3.37 KB
/
ADUnlockLoop.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#Loop on unlocking a specific user account
$User = '[email protected]' # UPN formatting
[int]$SeriesDelay = 5 #Delay in seconds, time between each series check of account status
[int]$ServerDelay = 0 #Delay in seconds, time between each servers check of account status
[int]$Loops = 0 # 0 = infinite
$LogOption = $true
$LogFolder = 'C:\Logs'
$LogLocation = $LogFolder + '\Unlock-' + ($User -split '@')[0] + '.txt'
$DomainControllers = (Get-ADGroupMember 'Domain Controllers').Name
#----------
Clear-Host
Write-Host "
=================
Unlock User Loop
=================
"
$User = Get-ADUser -Filter {UserPrincipalName -like $User}
if ($null -eq $User) {
Write-Host -ForegroundColor Red 'User does not exist
'
exit 1
} else {
Write-Host "User: $($User.SamAccountName)
"
}
function CheckLockStatus {
$Status = foreach ($Server in $DomainControllers) {
Start-Sleep $ServerDelay
#Write-Host "Checking server $Server"
[string]$Time = Get-Date -Format 'MM/dd/yyyy hh:mm tt'
$Check = (Get-ADUser $User -Properties LockedOut -Server $Server).LockedOut
if ($Check -eq $true) {
#$Check | Add-Member -MemberType NoteProperty -Name Server -Value $Server
Write-Host "User locked on $Server at $Time"
if ($LogOption -eq $true) {
"User locked on $Server at $Time" | Out-File -Append $LogLocaiton
}
}
$WholeObject = [PSCustomObject]@{
Server = $Server
Locked = $Check
}
$WholeObject
}
$Status
}
function CheckLockStatusIndividual {
$Server = $args[0]
$Status = (Get-ADUser $User -Properties LockedOut -Server $Server).LockedOut
$Status
}
function UnlockAccount {
Write-Host 'Unlocking account...'
$LockedServers = ($Status | Where-Object -Property Locked -eq $true).Server
foreach ($Server in $LockedServers) {
[string]$Time = Get-Date -Format 'MM/dd/yyyy hh:mm tt'
Unlock-ADAccount $User -Server $Server
$Status = CheckLockStatusIndividual $Server
if ($Status -eq $false) {
Write-Host "Unlocked account on server $Server at $Time"
if ($LogOption -eq $true) {
"Unlocked account on server $Server at $Time" | Out-File -Append $LogLocaiton
}
} else {
Write-Host "Failed to unlock account on server $Server at $Time"
if ($LogOption -eq $true) {
"Failed to unlock account on server $Server at $Time" | Out-File -Append $LogLocaiton
}
}
}
}
if ($Loops -eq 0) {
do {
if ($null -ne $Status) {
Remove-Variable Status
}
$i = 0
$Status = CheckLockStatus
if (($Status.Locked -eq $true.count -gt 0)) {
$Time = Get-Date -Format 'MM/dd/yyyy hh:mm tt'
#Write-Host "Account was locked at $($Time)"
UnlockAccount
}
Start-Sleep $SeriesDelay
} until ($i -eq 1)
} else {
$i = 0
do {
if ($null -ne $Status) {
Remove-Variable Status
}
$Status = CheckLockStatus
if (($Status.Locked -eq $true.count -gt 0)) {
$Time = Get-Date -Format 'MM/dd/yyyy hh:mm tt'
#Write-Host "Account was locked at $($Time)"
UnlockAccount
}
$i = $i + 1
Start-Sleep $SeriesDelay
} until ($i -eq $Loops)
}