[CmdletBinding()] param( [switch]$All, [switch]$IncludeDisabled, [string]$OutputPath, [switch]$Json ) $ErrorActionPreference = 'Stop' try { $tasks = Get-ScheduledTask -ErrorAction Stop } catch { Write-Warning "Не удалось прочитать планировщик: $($_.Exception.Message)" return } $rows = foreach ($task in $tasks) { $info = $null try { $info = Get-ScheduledTaskInfo -TaskPath $task.TaskPath -TaskName $task.TaskName -ErrorAction Stop } catch { $info = $null } $lastRun = $null $lastResult = $null if ($info) { $lastRun = $info.LastRunTime $lastResult = $info.LastTaskResult } $failed = ($null -ne $lastResult) -and ($lastResult -ne 0) $disabled = $task.State -eq 'Disabled' if ($All -or $failed -or ($IncludeDisabled -and $disabled)) { [pscustomobject]@{ Path = $task.TaskPath Name = $task.TaskName State = $task.State Author = $task.Author LastRunTime = $lastRun LastTaskResult = $lastResult Failed = $failed } } } $rows = $rows | Sort-Object Failed, Path, Name $failedCount = @($rows | Where-Object { $_.Failed }).Count Write-Host "Заданий в отчёте: $(@($rows).Count), с ошибкой: $failedCount" if ($Json) { $text = $rows | ConvertTo-Json -Depth 4 } else { $text = $rows | Format-Table -Wrap | Out-String -Width 260 } if ($OutputPath) { $text | Set-Content -LiteralPath $OutputPath -Encoding UTF8 Write-Host "Отчёт сохранён: $OutputPath" } else { $text }