-
Notifications
You must be signed in to change notification settings - Fork 0
/
LockoutStatus.psm1
137 lines (120 loc) · 2.96 KB
/
LockoutStatus.psm1
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
if (!(Get-FormatData "LockoutStatusData")) {
Update-FormatData -Append (Join-Path $PSScriptRoot `
lockoutstatus.format.ps1xml)
}
# Formats the date into the format we want it in.
Function Format-Date {
Param(
$Date
)
if ($Date -eq $null) {
""
} else {
$Date.ToString("d.M.yyyy HH:mm:ss")
}
}
Function Format-AccountExpiration {
Param(
$Date
)
if ($Date -and ((Get-Date) -gt $Date)) {
"True ({0})" -f ($Date.ToString("d.M.yyyy"))
} else {
"False"
}
}
# Formats the expiry data so that an empty set date
# doesn't show up as " (set )"
# i.e. when a DC is unavailable.
Function Format-PWExpiration {
Param(
$Expired,
$DateSet
)
if ($Date -eq "") {
""
} else {
[string]::format("{0} (set {1})", $Expired, $DateSet)
}
}
Function Get-LockoutStatus {
<#
.SYNOPSIS
Get credential information about a user
.DESCRIPTION
This cmdlet displays information about the selected user:
whether the user is locked out, when their password
was last set and if it's expired.
.EXAMPLE
Get-LockoutStatus "user"
.PARAMETER SamAccountName
The user to get information about.
#>
[CmdletBinding(
SupportsShouldProcess=$false,
ConfirmImpact="Low"
)]
Param(
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)]
$SamAccountName
)
begin {
if (!(Get-Module ActiveDirectory)) {
Import-Module ActiveDirectory
}
$DCs = Get-ADDomainController -Filter * |
select -expand HostName
$props = @{
Properties = @(
"AccountExpirationDate",
"LockedOut",
"badPwdCount",
"AccountLockoutTime",
"LastBadPasswordAttempt",
"LastLogonDate",
"PasswordExpired",
"PasswordLastSet"
)
}
}
process {
$results = @()
foreach ($DC in $DCs) {
try {
$user = Get-ADUser -Identity $SamAccountName -Server $DC @props
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException] {
"User not found on $DC."
continue
}
catch [Microsoft.ActiveDirectory.Management.ADServerDownException] {
"Unable to connect to $DC."
continue
}
catch {
Write-Debug "Unknown error: $($_.Exception.GetType().FullName)"
throw $_
}
$result = New-Object PSObject -Property @{
User = $user.SamAccountName;
Name = $user.Name;
Disabled = !$user.Enabled;
Expired = Format-AccountExpiration $user.AccountExpirationDate;
DomainController = $DC.split(".")[0];
Locked = $user.LockedOut;
Count = $user.badPwdCount;
LastLock = Format-Date $user.AccountLockoutTime;
LastBadAttempt = Format-Date $user.LastBadPasswordAttempt;
LastLogonDate = Format-Date $user.LastLogonDate;
PwdExpired = Format-PWExpiration $user.PasswordExpired `
(Format-Date $user.PasswordLastSet)
}
$result.PSTypeNames.Insert(0, "LockoutStatusData")
$results += $result
}
$results
}
end {
}
}
Export-ModuleMember Get-LockoutStatus