Hi I have created a powershell script to monitor event logs for account lock out or account disable, but alert is not generated , can anyone please help me tgom correct my powershell script as in am using powershell script monitor to monitor the logs.
$LogName = "Security"
$EventIDs = @(4740, 4725) # 4740: lockout, 4725: disable
$Minutes = 15
$TimeFrame = (Get-Date).AddMinutes(-$Minutes)
Define target service accounts (case-insensitive)
$TargetAccounts = @("svc_solarwinds")
Get matching events
$Events = Get-WinEvent -FilterHashtable @{
LogName = $LogName
Id = $EventIDs
StartTime = $TimeFrame
} -ErrorAction SilentlyContinue
$MatchCount = 0
if ($Events -and $Events.Count -gt 0) {
foreach ($ev in $Events) {
$eventID = $ev.Id
$timeCreated = $ev.TimeCreated
$message = $ev.Message
# Extract account name using regex
$AccountMatch = $message -match "Account Name:\s+([^\s]+)"
$AccountName = if ($AccountMatch) { $Matches[1] } else { "UNKNOWN" }
# Check if account matches target list
if ($TargetAccounts -contains $AccountName.ToLower()) {
$MatchCount++
Write-Output "Message: '$AccountName' at $($ev.TimeCreated) with message $($ev.Message)"
Write-Output "Statistic :1"
}
}
}
if ($MatchCount -eq 0) {
Write-Output "Message : No service account lockout/disable events found in last $Minutes minutes."
Write-Output "Statistic :0"
}