Skip to content

Instantly share code, notes, and snippets.

@jamil666
Created December 3, 2019 09:28
Show Gist options
  • Save jamil666/2acccd04a58f034b6414d173e740f850 to your computer and use it in GitHub Desktop.
Save jamil666/2acccd04a58f034b6414d173e740f850 to your computer and use it in GitHub Desktop.
Powershell script for finding user lockout source
## Define the username that’s locked out
$Username = Read-Host "Please enter username: "
## Find the domain controller PDCe role
$Pdce = (Get-AdDomain).PDCEmulator
# Check if user locked out
$LocketOutStatus = Get-ADUser -Properties LockedOut -Identity $Username | select LockedOut
if ($LocketOutStatus.LockedOut -eq 'True'){
## Build the parameters to pass to Get-WinEvent
$GweParams = @{
‘Computername’ = $Pdce
‘LogName’ = ‘Security’
‘FilterXPath’ = "*[System[EventID=4740] and EventData[Data[@Name='TargetUserName']='$Username']]"
}
## Query the security event log
$Events = Get-WinEvent @GweParams -Credential (Get-Credential)
Write-Host -ForegroundColor Green ($Events | foreach {$_.Properties[1].value + ' ' + $_.TimeCreated})
}
else{
Write-Host "User" $Username "not locked" -ForegroundColor Green
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment