$auditPath = "C:\Audit"
if (-not (Test-Path $auditPath)) { New-Item -ItemType Directory -Path $auditPath | Out-Null }
Write-Host "=== ЧАСТЬ 1: НЕАКТИВНЫЕ АККАУНТЫ ===" -ForegroundColor Cyan
Write-Host "`n--- Отключённые аккаунты ---" -ForegroundColor Yellow
$disabled = Get-ADUser -Filter {Enabled -eq $false} -Properties WhenChanged | Select-Object Name, SamAccountName, WhenChanged
$disabled | Format-Table -AutoSize
$disabled | Export-Csv "$auditPath\1_otklyuchennye.csv" -NoTypeInformation -Encoding UTF8
Write-Host "`n--- Ни разу не входили ---" -ForegroundColor Yellow
$nikogda = Get-ADUser -Filter {Enabled -eq $true} -Properties LastLogonTimeStamp | Where-Object { -not $_.LastLogonTimeStamp } | Select-Object Name, SamAccountName
$nikogda | Format-Table -AutoSize
$nikogda | Export-Csv "$auditPath\3_ni_razu.csv" -NoTypeInformation -Encoding UTF8
Write-Host "`n=== ЧАСТЬ 2: ПРОВЕРКА ПРАВ ===" -ForegroundColor Cyan
$matrica = @{
"Бухгалтер" = @("Accounting", "1C_Users")
"Менеджер" = @("Sales", "CRM_Users")
"Системный администратор" = @("IT_Admins")
}
$narusheniya = @()
$users = Get-ADUser -Filter {Enabled -eq $true} -Properties Title, MemberOf
foreach ($u in $users) {
if (-not $u.Title) { continue }
$fakt = @($u.MemberOf | ForEach-Object { (Get-ADGroup $_).Name })
$razresheno = $matrica[$u.Title]
if (-not $razresheno) { continue }
foreach ($g in $fakt) {
if ($g -notin $razresheno) {
$narusheniya += [PSCustomObject]@{ Сотрудник=$u.Name; Должность=$u.Title; ЛишняяГруппа=$g; Положено=($razresheno -join ", ") }
}
}
}
Write-Host "`n--- Выявленные нарушения ---" -ForegroundColor Red
$narusheniya | Format-Table -AutoSize
$narusheniya | Export-Csv "$auditPath\4_narusheniya_prav.csv" -NoTypeInformation -Encoding UTF8
Write-Host "`n=== АУДИТ ЗАВЕРШЁН. Отчёты в $auditPath ===" -ForegroundColor Green