Skip to content

Commit a57a7ea

Browse files
committed
✨refactor: standardize function formatting and improve error handling in Get-LockedOutLocation
1 parent d233d3d commit a57a7ea

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

Active Directory/AD Users/Get-LockedOutLocation.ps1

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
1-
#Requires -Version 2.0
2-
Function Get-LockedOutLocation {
1+
function Get-LockedOutLocation {
32
<#
43
.SYNOPSIS
5-
This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out.
4+
This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out.
65
76
.DESCRIPTION
8-
This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out.
9-
The locked out location is found by querying the PDC Emulator for locked out events (4740).
10-
The function will display the BadPasswordTime attribute on all of the domain controllers to add in further troubleshooting.
7+
This function will locate the computer that processed a failed user logon attempt which caused the user account to become locked out.
8+
The locked out location is found by querying the PDC Emulator for locked out events (4740).
9+
The function will display the BadPasswordTime attribute on all of the domain controllers to add in further troubleshooting.
1110
1211
.EXAMPLE
13-
PS C:\>Get-LockedOutLocation -Identity Joe.Davis
12+
PS C:\>Get-LockedOutLocation -Identity Joe.Davis
1413
1514
16-
This example will find the locked out location for Joe Davis.
15+
This example will find the locked out location for Joe Davis.
1716
.NOTE
18-
This function is only compatible with an environment where the domain controller with the PDCe role to be running Windows Server 2008 SP2 and up.
19-
The script is also dependent the ActiveDirectory PowerShell module, which requires the AD Web services to be running on at least one domain controller.
20-
Author:Jason Walker
21-
Last Modified: 3/20/2013
17+
This function is only compatible with an environment where the domain controller with the PDCe role to be running Windows Server 2008 SP2 and up.
18+
The script is also dependent the ActiveDirectory PowerShell module, which requires the AD Web services to be running on at least one domain controller.
19+
Author:Jason Walker
20+
Last Modified: 3/20/2013
2221
#>
2322
[CmdletBinding()]
2423

25-
Param(
24+
param(
2625
[Parameter(Mandatory = $True)]
2726
[String]$Identity
2827
)
2928

30-
Begin {
29+
begin {
3130
$DCCounter = 0
3231
$LockedOutStats = @()
3332

34-
Try {
33+
try {
3534
Import-Module ActiveDirectory -ErrorAction Stop
36-
} Catch {
35+
} catch {
3736
Write-Warning $_
38-
Break
37+
break
3938
}
4039
}#end begin
41-
Process {
40+
process {
4241

4342
#Get all domain controllers in domain
4443
$DomainControllers = Get-ADDomainController -Filter *
4544
$PDCEmulator = ($DomainControllers | Where-Object { $_.OperationMasterRoles -contains 'PDCEmulator' })
4645

4746
Write-Verbose 'Finding the domain controllers in the domain'
48-
Foreach ($DC in $DomainControllers) {
47+
foreach ($DC in $DomainControllers) {
4948
$DCCounter++
5049
Write-Progress -Activity 'Contacting DCs for lockout info' -Status "Querying $($DC.Hostname)" -PercentComplete (($DCCounter / $DomainControllers.Count) * 100)
51-
Try {
50+
try {
5251
$UserInfo = Get-ADUser -Identity $Identity -Server $DC.Hostname -Properties AccountLockoutTime, LastBadPasswordAttempt, BadPwdCount, LockedOut -ErrorAction Stop
53-
} Catch {
52+
} catch {
5453
Write-Warning $_
55-
Continue
54+
continue
5655
}
57-
If ($UserInfo.LastBadPasswordAttempt) {
56+
if ($UserInfo.LastBadPasswordAttempt) {
5857
$LockedOutStats += New-Object -TypeName PSObject -Property @{
5958
Name = $UserInfo.SamAccountName
6059
SID = $UserInfo.SID.Value
@@ -70,18 +69,18 @@ Function Get-LockedOutLocation {
7069
$LockedOutStats | Format-Table -Property Name, LockedOut, DomainController, BadPwdCount, AccountLockoutTime, LastBadPasswordAttempt -AutoSize
7170

7271
#Get User Info
73-
Try {
72+
try {
7473
Write-Verbose "Querying event log on $($PDCEmulator.HostName)"
7574
$LockedOutEvents = Get-WinEvent -ComputerName $PDCEmulator.HostName -FilterHashtable @{LogName = 'Security'; Id = 4740 } -ErrorAction Stop | Sort-Object -Property TimeCreated -Descending
76-
} Catch {
75+
} catch {
7776
Write-Warning $_
78-
Continue
77+
continue
7978
}#end catch
8079

81-
Foreach ($Event in $LockedOutEvents) {
82-
If ($Event | Where-Object { $_.Properties[2].value -match $UserInfo.SID.Value }) {
80+
foreach ($item in $LockedOutEvents) {
81+
if ($item | Where-Object { $_.Properties[2].value -match $UserInfo.SID.Value }) {
8382

84-
$Event | Select-Object -Property @(
83+
$item | Select-Object -Property @(
8584
@{Label = 'User'; Expression = { $_.Properties[0].Value } }
8685
@{Label = 'DomainController'; Expression = { $_.MachineName } }
8786
@{Label = 'EventId'; Expression = { $_.Id } }
@@ -90,9 +89,9 @@ Function Get-LockedOutLocation {
9089
@{Label = 'LockedOutLocation'; Expression = { $_.Properties[1].Value } }
9190
)
9291

93-
}#end ifevent
92+
}#end if event
9493

95-
}#end foreach lockedout event
94+
}#end foreach lockout event
9695

9796
}#end process
9897

0 commit comments

Comments
 (0)