#Requires -Modules ActiveDirectory <# .SYNOPSIS Wyszukuje nieaktywne konta użytkowników i komputerów w AD. .DESCRIPTION Identyfikuje konta, które nie logowały się przez określoną liczbę dni. Obsługuje konta użytkowników i komputerów osobno. Opcjonalnie: wyłącza lub przenosi do OU kwarantanny. .PARAMETER Days Liczba dni nieaktywności (domyślnie: 90). .PARAMETER Type Typ kont: Users, Computers lub Both (domyślnie: Both). .PARAMETER SearchBase DN OU do przeszukania. .PARAMETER DisableInactive Przełącznik — wyłącza znalezione nieaktywne konta (wymaga potwierdzenia). .PARAMETER ExportCSV Ścieżka do pliku CSV z raportem. .EXAMPLE .\Get-ADInactiveAccounts.ps1 -Days 90 -Type Users .\Get-ADInactiveAccounts.ps1 -Days 180 -ExportCSV C:\Temp\inactive.csv #> param( [int]$Days = 90, [ValidateSet('Users', 'Computers', 'Both')] [string]$Type = 'Both', [string]$SearchBase = "", [switch]$DisableInactive, [string]$ExportCSV = "" ) Import-Module ActiveDirectory -ErrorAction Stop $cutoff = (Get-Date).AddDays(-$Days) Write-Host "`n Szukam kont nieaktywnych od $Days dni (przed $($cutoff.ToString('yyyy-MM-dd')))..." -ForegroundColor Cyan $allResults = @() if ($Type -in 'Users', 'Both') { $userParams = @{ Filter = { Enabled -eq $true -and LastLogonDate -lt $cutoff } Properties = 'LastLogonDate', 'DisplayName', 'Department', 'Description', 'Created', 'DistinguishedName' } if ($SearchBase) { $userParams.SearchBase = $SearchBase } $inactiveUsers = Get-ADUser @userParams | Sort-Object LastLogonDate Write-Host "`n[UŻYTKOWNICY] Nieaktywni: $($inactiveUsers.Count)" -ForegroundColor Yellow if ($inactiveUsers) { $inactiveUsers | Select-Object SamAccountName, DisplayName, Department, @{N='LastLogon'; E={ $_.LastLogonDate?.ToString("yyyy-MM-dd") }}, @{N='DaysInactive'; E={ [math]::Round(((Get-Date) - $_.LastLogonDate).TotalDays) }}, @{N='Created'; E={ $_.Created.ToString("yyyy-MM-dd") }} | Format-Table -AutoSize $allResults += $inactiveUsers | Select-Object @{N='Type';E={'User'}}, SamAccountName, DisplayName, Department, LastLogonDate, @{N='DaysInactive'; E={ [math]::Round(((Get-Date) - $_.LastLogonDate).TotalDays) }}, DistinguishedName } } if ($Type -in 'Computers', 'Both') { $compParams = @{ Filter = { Enabled -eq $true -and LastLogonDate -lt $cutoff } Properties = 'LastLogonDate', 'OperatingSystem', 'Description', 'Created', 'DistinguishedName' } if ($SearchBase) { $compParams.SearchBase = $SearchBase } $inactiveComps = Get-ADComputer @compParams | Sort-Object LastLogonDate Write-Host "`n[KOMPUTERY] Nieaktywne: $($inactiveComps.Count)" -ForegroundColor Yellow if ($inactiveComps) { $inactiveComps | Select-Object Name, OperatingSystem, Description, @{N='LastLogon'; E={ $_.LastLogonDate?.ToString("yyyy-MM-dd") }}, @{N='DaysInactive'; E={ [math]::Round(((Get-Date) - $_.LastLogonDate).TotalDays) }} | Format-Table -AutoSize $allResults += $inactiveComps | Select-Object @{N='Type';E={'Computer'}}, @{N='SamAccountName';E={$_.Name}}, @{N='DisplayName';E={$_.Name}}, @{N='Department';E={$_.OperatingSystem}}, LastLogonDate, @{N='DaysInactive'; E={ [math]::Round(((Get-Date) - $_.LastLogonDate).TotalDays) }}, DistinguishedName } } if ($ExportCSV -and $allResults) { $allResults | Export-Csv -Path $ExportCSV -NoTypeInformation -Encoding UTF8 Write-Host "`n Wyeksportowano $($allResults.Count) wpisów do: $ExportCSV" -ForegroundColor Green } if ($DisableInactive -and $allResults) { $confirm = Read-Host "`nWYŁĄCZYĆ $($allResults.Count) nieaktywnych kont? Wpisz 'TAK' aby potwierdzić" if ($confirm -eq 'TAK') { foreach ($acc in $allResults) { Disable-ADAccount -Identity $acc.SamAccountName -ErrorAction SilentlyContinue Write-Host " [OK] Wyłączono: $($acc.SamAccountName)" } Write-Host "`nWyłączono $($allResults.Count) kont." -ForegroundColor Green } else { Write-Host "Anulowano." -ForegroundColor Yellow } }