Hi all,
I have this script
type or paste c$SamAccountName = Read-Host -Prompt 'Please Enter User Name'
while ($SamAccountName -ne 'Done')
{
if ($SamAccountName -eq "")
{
Write-Host -ForegroundColor Magenta "User name annot be blank!"
}
else
{
$accountExist = [bool] (Get-ADUser -Filter { SamAccountName -eq $SamAccountName })
if ($accountExist -eq "true" ){
# The account exist and now we need to see if its locked out.
# Locked
if ( (Get-ADUser $SamAccountName -Properties * | Select-Object LockedOut) -match "True" )
{
$PDC = (Get-ADDomainController -Filter * | Where-Object {$_.OperationMasterRoles -contains "PDCEmulator"})
#Get user info
$UserInfo = Get-ADUser -Identity $SamAccountName
#Search PDC for lockout events with ID 4740
$LockedOutEvents = Get-WinEvent -ComputerName $PDC -FilterHashtable @{LogName='Security';Id=4740} -ErrorAction Stop | Sort-Object -Property TimeCreated -Unique | Select-Object -First 2
#Parse and filter out lockout events
Foreach($Event in $LockedOutEvents)
{
If($Event | Where {$_.Properties[2].value -match $UserInfo.SID.Value})
{n
$Event | Select-Object -Property @(
@{Label = 'User Name'; Expression = {$_.Properties[0].Value}}
@{Label = 'Domain Controller'; Expression = {$_.MachineName}}
@{Label = 'Lockout Time Stamp'; Expression = {$_.TimeCreated}}
@{Label = 'Message'; Expression = {$_.Message -split "`r" | Select -First 1}}
@{Label = 'Lockout Source'; Expression = {$_.Properties[1].Value}}
)
}
}
Write-Host -ForegroundColor Yellow "The account '$SamAccountName' is locked."
#
}
if ( (Get-ADUser $SamAccountName -Properties * | Select-Object LockedOut) -match "False" )
{
Write-Host -ForegroundColor Green "The account '$SamAccountName' is not locked."
}
}
else {
Write-Host -ForegroundColor Red "The account "$SamAccountName" does not exist please re-enter username. "
}
}
$SamAccountName = Read-Host -Prompt 'Please Enter User Name'
}
Read-Host -Prompt 'Press enter to exit'ode here
that should tell me if the user exists in AD and if it’s locked out or not if the user locked out the PowerShell should tell me the lockout source device but it’s not working can some help? thanks