Created
September 22, 2019 13:20
-
-
Save nirinium/f229ded55fae9f88434b75f015dc7c7d to your computer and use it in GitHub Desktop.
lockout.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Import-Module ActiveDirectory | |
$UserName = Read-Host "Please enter username" | |
#Get main DC | |
$PDC = (Get-ADDomainController -Filter * | Where-Object {$_.OperationMasterRoles -contains "PDCEmulator"}) | |
#Get user info | |
$UserInfo = Get-ADUser -Identity $UserName | |
#Search PDC for lockout events with ID 4740 | |
$LockedOutEvents = Get-WinEvent -ComputerName $PDCEmulator.HostName -FilterHashtable @{LogName='Security';Id=4740} -ErrorAction Stop | Sort-Object -Property TimeCreated -Descending | |
#Parse and filter out lockout events | |
Foreach($Event in $LockedOutEvents) | |
{ | |
If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value}) | |
{ | |
$Event | Select-Object -Property @( | |
@{Label = 'User'; Expression = {$_.Properties[0].Value}} | |
@{Label = 'DomainController'; Expression = {$_.MachineName}} | |
@{Label = 'EventId'; Expression = {$_.Id}} | |
@{Label = 'LockoutTimeStamp'; Expression = {$_.TimeCreated}} | |
@{Label = 'Message'; Expression = {$_.Message -split "`r" | Select -First 1}} | |
@{Label = 'LockoutSource'; Expression = {$_.Properties[1].Value}} | |
) | |
}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment