[CmdletBinding()] param( [ValidateRange(1, 90)] [int]$Days = 7, [ValidateRange(1, 20000)] [int]$MaxEvents = 5000, [string]$Csv ) $ErrorActionPreference = 'Stop' $identity = [Security.Principal.WindowsIdentity]::GetCurrent() $principal = New-Object Security.Principal.WindowsPrincipal($identity) if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 'Нужны права администратора для чтения журнала Security.' } $start = (Get-Date).AddDays(-$Days) $failed = @() $locked = @() try { $failed = @(Get-WinEvent -FilterHashtable @{ LogName = 'Security'; Id = 4625; StartTime = $start } -MaxEvents $MaxEvents -ErrorAction Stop) } catch { Write-Host "События 4625 за период не найдены или журнал недоступен: $_" } try { $locked = @(Get-WinEvent -FilterHashtable @{ LogName = 'Security'; Id = 4740; StartTime = $start } -MaxEvents $MaxEvents -ErrorAction Stop) } catch { Write-Host "События 4740 за период не найдены или журнал недоступен: $_" } $rows = foreach ($event in $failed) { $props = $event.Properties [pscustomobject]@{ Time = $event.TimeCreated User = if ($props.Count -gt 5) { $props[5].Value } else { '' } Domain = if ($props.Count -gt 6) { $props[6].Value } else { '' } Status = if ($props.Count -gt 7) { $props[7].Value } else { '' } SubStatus = if ($props.Count -gt 9) { $props[9].Value } else { '' } LogonType = if ($props.Count -gt 10) { $props[10].Value } else { '' } IpAddress = if ($props.Count -gt 19) { $props[19].Value } else { '' } } } $rows = @($rows) Write-Host ("Неудачных входов: {0}, блокировок: {1}, период: {2} дней" -f $rows.Count, $locked.Count, $Days) if ($rows.Count -gt 0) { Write-Host 'По пользователям:' $rows | Group-Object User | Sort-Object Count -Descending | Select-Object Count, Name | Format-Table -AutoSize Write-Host 'По IP-адресам:' $rows | Group-Object IpAddress | Sort-Object Count -Descending | Select-Object Count, Name | Format-Table -AutoSize } if ($Csv) { $parent = Split-Path -Parent $Csv if ($parent -and -not (Test-Path -LiteralPath $parent)) { New-Item -ItemType Directory -Path $parent | Out-Null } $rows | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "Отчёт сохранён: $Csv" } Write-Host 'Журнал Security только читался, ничего не изменялось.'