compact report output
This commit is contained in:
+57
-32
@@ -153,9 +153,10 @@ function Get-SystemCleanupOS {
|
||||
}
|
||||
|
||||
function Write-SystemCleanupMessage {
|
||||
param([string]$Message)
|
||||
param([string]$Message, [switch]$Detail)
|
||||
# Host/information stream keeps logs out of helper return values, even with Tools loaded.
|
||||
Write-Host "[SystemCleanup] $Message"
|
||||
if ($Detail) { Write-Verbose "[SystemCleanup] $Message" }
|
||||
else { Write-Host "[SystemCleanup] $Message" }
|
||||
}
|
||||
|
||||
function Test-SystemCleanupPath {
|
||||
@@ -263,21 +264,21 @@ function Invoke-SystemCleanupCategory {
|
||||
if ($blocked) {
|
||||
$Context.Stats[$Name].Status = 'Blocked on server'
|
||||
$Context.Stats[$Name].EstimateStatus = 'Not assessed'
|
||||
Write-SystemCleanupMessage "BLOCKED on server OS: $Name ($setting)"
|
||||
Write-SystemCleanupMessage "BLOCKED on server OS: $Name ($setting)" -Detail:$Context.Options.ReportOnly
|
||||
return
|
||||
}
|
||||
if ($Context.Options.ReportOnly) {
|
||||
if (-not $Context.ContainsKey('Candidates')) { $Context.Candidates = @{} }
|
||||
$Context.Candidates[$Name] = New-Object 'System.Collections.Generic.Dictionary[string,long]' ([StringComparer]::OrdinalIgnoreCase)
|
||||
$selection = if ($enabled) { 'enabled for apply' } else { 'disabled; assessment only' }
|
||||
Write-SystemCleanupMessage "Assessing $Name ($selection)."
|
||||
Write-SystemCleanupMessage "Assessing $Name ($selection)." -Detail
|
||||
}
|
||||
try { & $Action } catch {
|
||||
$Context.Stats[$Name].Status = 'Failed'
|
||||
$Context.Stats[$Name].EstimateStatus = 'Incomplete'
|
||||
$Context.Stats[$Name].Notes = $_.Exception.Message
|
||||
$Context.Errors++
|
||||
Write-SystemCleanupMessage "$Name failed: $($_.Exception.Message)"
|
||||
Write-SystemCleanupMessage "$Name failed: $($_.Exception.Message)" -Detail:$Context.Options.ReportOnly
|
||||
}
|
||||
if (-not $Context.Options.ReportOnly) {
|
||||
$s = $Context.Stats[$Name]
|
||||
@@ -291,7 +292,7 @@ function Set-SystemCleanupSkipped {
|
||||
$Context.Stats[$Category].Status = $Status
|
||||
$Context.Stats[$Category].EstimateStatus = 'Not assessed'
|
||||
$Context.Stats[$Category].Notes = $Reason
|
||||
Write-SystemCleanupMessage $Reason
|
||||
Write-SystemCleanupMessage $Reason -Detail:$Context.Options.ReportOnly
|
||||
}
|
||||
|
||||
function Get-SystemCleanupReportTotals {
|
||||
@@ -316,36 +317,55 @@ function Get-SystemCleanupReportTotals {
|
||||
|
||||
function Write-SystemCleanupReport {
|
||||
param($Context)
|
||||
Write-SystemCleanupMessage 'REPORT ONLY: no cleanup performed. Category estimates use the configured retention periods.'
|
||||
Write-SystemCleanupMessage 'REPORT ONLY - no cleanup performed.'
|
||||
$totals = Get-SystemCleanupReportTotals $Context
|
||||
Write-SystemCleanupMessage ('Known savings: ENABLED {0:N2} MiB ({1:N0} files); additional DISABLED {2:N2} MiB ({3:N0} files).' -f ($totals.EnabledBytes / 1MB), $totals.EnabledFiles, ($totals.AdditionalDisabledBytes / 1MB), $totals.AdditionalDisabledFiles)
|
||||
Write-SystemCleanupMessage "Retention: temp $($Context.Options.TempDays)d; diagnostics $($Context.Options.DiagnosticDays)d; backups $($Context.Options.BackupDays)d."
|
||||
Write-SystemCleanupMessage ('{0,-25} {1,8} {2,11} {3}' -f 'Category', 'Files', 'MiB', 'Notes')
|
||||
foreach ($group in @('Enabled', 'Disabled', 'Blocked')) {
|
||||
$rows = @($Context.Stats.Values | Where-Object {
|
||||
if ($group -eq 'Blocked') { $_.Status -eq 'Blocked on server' }
|
||||
else { $_.Status -ne 'Blocked on server' -and $_.Enabled -eq ($group -eq 'Enabled') }
|
||||
} | Sort-Object Category)
|
||||
$heading = switch ($group) {
|
||||
Enabled { 'ENABLED - eligible for cleanup when CleanupReportOnly=false, subject to the listed conditions' }
|
||||
Disabled { 'DISABLED - potential savings only; these categories will not run until enabled' }
|
||||
Blocked { 'BLOCKED ON SERVER - not assessed and cannot be enabled on this OS' }
|
||||
Enabled { 'ENABLED - would run with current settings' }
|
||||
Disabled { 'DISABLED - potential only; will not run' }
|
||||
Blocked { 'BLOCKED ON SERVER - cannot run' }
|
||||
}
|
||||
Write-SystemCleanupMessage $heading
|
||||
if (-not $rows.Count) { Write-SystemCleanupMessage ' None.' }
|
||||
foreach ($row in $rows) {
|
||||
$estimate = switch ($row.EstimateStatus) {
|
||||
'Not estimated' { 'space savings not estimated' }
|
||||
'Not assessed' { 'space savings not assessed' }
|
||||
default {
|
||||
if ($null -eq $row.CandidateBytes) { 'space savings unavailable' }
|
||||
else { '{0} eligible files / {1:N2} MiB' -f $row.CandidateFiles, ($row.CandidateBytes / 1MB) }
|
||||
}
|
||||
$files = '-'; $size = '-'; $notes = @()
|
||||
if ($row.EstimateStatus -eq 'Not estimated') { $notes += 'not estimated' }
|
||||
elseif ($row.EstimateStatus -eq 'Not assessed') { $notes += 'not assessed' }
|
||||
elseif ($null -ne $row.CandidateBytes) {
|
||||
$files = '{0:N0}' -f $row.CandidateFiles
|
||||
$size = if ($row.CandidateBytes -gt 0 -and $row.CandidateBytes -lt (0.01 * 1MB)) { '<0.01' } else { '{0:N2}' -f ($row.CandidateBytes / 1MB) }
|
||||
} else { $notes += 'unavailable' }
|
||||
if ($row.Status -notin @('Preview', 'Blocked on server')) { $notes += $row.Status }
|
||||
if ($row.EstimateStatus -eq 'Incomplete') { $notes += 'incomplete' }
|
||||
if ($row.FailedFiles) { $notes += "$($row.FailedFiles) files skipped" }
|
||||
if ($row.Category -eq 'DiskCleanup') {
|
||||
$notes += "$($Context.Options.DiskCleanupPreset) ($(@($Context.Options.DiskCleanupItems).Count) handlers)"
|
||||
if (-not $row.Enabled) { $notes += 'will not run' }
|
||||
# The selected handler list is configuration detail, not a warning.
|
||||
if ($row.Status -eq 'Failed') { $notes += $row.Notes }
|
||||
else { Write-SystemCleanupMessage "Disk Cleanup: $($row.Notes)" -Detail }
|
||||
} elseif ($row.Category -eq 'ComponentStore') {
|
||||
$notes += 'background task on apply'
|
||||
if ($row.Notes) { $notes += $row.Notes }
|
||||
} elseif ($row.Notes) { $notes += $row.Notes }
|
||||
# Keep every category on one line, including multiline native errors.
|
||||
$noteText = ($notes -join '; ') -replace '\s+', ' '
|
||||
if ($noteText.Length -gt 180) {
|
||||
Write-SystemCleanupMessage "$($row.Category): $noteText" -Detail
|
||||
$noteText = $noteText.Substring(0, 163) + '... (see verbose)'
|
||||
}
|
||||
if ($row.EstimateStatus -eq 'Incomplete') { $estimate += ' (incomplete assessment)' }
|
||||
Write-SystemCleanupMessage " $($row.Category) [$($row.Setting)]: $estimate; $($row.Status). $($row.Notes)"
|
||||
Write-SystemCleanupMessage ('{0,-25} {1,8} {2,11} {3}' -f $row.Category, $files, $size, $noteText.Trim())
|
||||
}
|
||||
}
|
||||
$totals = Get-SystemCleanupReportTotals $Context
|
||||
Write-SystemCleanupMessage ('Known unique file estimates in ENABLED categories: {0} files / {1:N2} MiB.' -f $totals.EnabledFiles, ($totals.EnabledBytes / 1MB))
|
||||
Write-SystemCleanupMessage ('Additional unique file estimates in DISABLED categories, beyond enabled categories: {0} files / {1:N2} MiB.' -f $totals.AdditionalDisabledFiles, ($totals.AdditionalDisabledBytes / 1MB))
|
||||
Write-SystemCleanupMessage 'Category estimates can overlap; the totals count each path once. Native savings and unassessed data are excluded; actual freed disk space may differ.'
|
||||
Write-SystemCleanupMessage 'Totals count each path once; rows may overlap. Unknown/unassessed savings excluded; disk space freed may differ.'
|
||||
Write-SystemCleanupMessage 'Settings: Cleanup<Category>; DiskCleanup uses CleanupDiskCleanupItems. Full diagnostics: $VerbosePreference="Continue".'
|
||||
}
|
||||
|
||||
function Test-SystemCleanupAppRunning {
|
||||
@@ -359,7 +379,7 @@ function Invoke-SystemCleanupProcess {
|
||||
$nativeDir = if ([Environment]::Is64BitOperatingSystem -and -not [Environment]::Is64BitProcess) { 'Sysnative' } else { 'System32' }
|
||||
$executable = Join-Path $env:SystemRoot "$nativeDir\$FileName"
|
||||
if (-not (Test-Path -LiteralPath $executable -PathType Leaf)) { throw "Unavailable: $executable" }
|
||||
Write-SystemCleanupMessage "$FileName $Arguments (native operation; size not estimated)"
|
||||
Write-SystemCleanupMessage "$FileName $Arguments (native operation; size not estimated)" -Detail:$Context.Options.ReportOnly
|
||||
if ($Context.Options.ReportOnly) { return }
|
||||
$process = Start-Process -FilePath $executable -ArgumentList $Arguments -WindowStyle Hidden -Wait -PassThru -ErrorAction Stop
|
||||
if ($process.ExitCode -ne 0) { throw "$FileName returned exit code $($process.ExitCode)." }
|
||||
@@ -473,10 +493,10 @@ function Invoke-SystemCleanupDiskCleanup {
|
||||
# Defence in depth: even direct helper calls cannot run cleanmgr on a server.
|
||||
if ($Context.OS.IsServer) { throw 'Disk Cleanup is blocked on server OSes.' }
|
||||
$selected = @($Context.Options.DiskCleanupItems)
|
||||
Write-SystemCleanupMessage "Disk Cleanup selection: $($Context.Options.DiskCleanupPreset); selected items: $($selected -join ', '). Windows controls retention for these handlers."
|
||||
Write-SystemCleanupMessage "Disk Cleanup selection: $($Context.Options.DiskCleanupPreset); selected items: $($selected -join ', '). Windows controls retention for these handlers." -Detail
|
||||
if ($Context.Options.ReportOnly) {
|
||||
$unselected = @((Get-SystemCleanupHandlers) | Where-Object { $_ -notin $selected })
|
||||
Write-SystemCleanupMessage "Disk Cleanup items not selected (not run during apply): $($unselected -join ', '). Handler savings are not estimated."
|
||||
Write-SystemCleanupMessage "Disk Cleanup items not selected (not run during apply): $($unselected -join ', '). Handler savings are not estimated." -Detail
|
||||
return
|
||||
}
|
||||
if (-not $selected.Count) { return }
|
||||
@@ -535,7 +555,7 @@ function Invoke-SystemCleanupSearchCompaction {
|
||||
if (-not (Test-Path -LiteralPath $database -PathType Leaf)) { return }
|
||||
if (-not (Test-SystemCleanupPath $database (Split-Path -Parent $database))) { return }
|
||||
if ($Context.Options.ReportOnly) {
|
||||
Write-SystemCleanupMessage "Would compact $database; size savings cannot be estimated."
|
||||
Write-SystemCleanupMessage "Would compact $database; size savings cannot be estimated." -Detail
|
||||
return
|
||||
}
|
||||
$drive = New-Object IO.DriveInfo ([IO.Path]::GetPathRoot($database))
|
||||
@@ -619,7 +639,10 @@ function Invoke-SystemCleanup {
|
||||
# Do not stop Windows servicing to reclaim a log. Remove only when already idle.
|
||||
if ($total -ge 200MB -and (Get-Service -Name TrustedInstaller -ErrorAction Stop).Status -eq 'Stopped') {
|
||||
Clear-SystemCleanupTree $context 'ActiveCBSLog' $cbs $diagnosticCutoff -Include @('CBS.log')
|
||||
} else { Write-SystemCleanupMessage 'Active CBS.log retained: servicing is active or archives total less than 200 MiB.' }
|
||||
} else {
|
||||
$context.Stats.ActiveCBSLog.Notes = 'Retained: servicing active or CBS archives below 200 MiB.'
|
||||
Write-SystemCleanupMessage 'Active CBS.log retained: servicing is active or archives total less than 200 MiB.' -Detail:$Options.ReportOnly
|
||||
}
|
||||
}
|
||||
Invoke-SystemCleanupCategory $context 'CBSArchives' {
|
||||
Clear-SystemCleanupTree $context 'CBSArchives' (Join-Path $env:SystemRoot 'Logs\CBS') $diagnosticCutoff -Include @('CbsPersist_*.log', 'CbsPersist_*.cab')
|
||||
@@ -686,7 +709,7 @@ function Invoke-SystemCleanup {
|
||||
$context.Stats.BrowserCache.Status = 'Partial'
|
||||
$context.Stats.BrowserCache.EstimateStatus = 'Incomplete'
|
||||
$context.Stats.BrowserCache.Notes += "$($browser.Name) is running; its cache was not assessed. "
|
||||
Write-SystemCleanupMessage "$($browser.Name) is running; its caches retained."
|
||||
Write-SystemCleanupMessage "$($browser.Name) is running; its caches retained." -Detail:$Options.ReportOnly
|
||||
continue
|
||||
}
|
||||
foreach ($profile in $profiles) {
|
||||
@@ -716,12 +739,12 @@ function Invoke-SystemCleanup {
|
||||
if ($Options.ReportOnly) { Set-SystemCleanupSkipped $context 'DeliveryOptimization' 'Delivery Optimization cmdlet is not installed.' -Status 'Unavailable'; return }
|
||||
throw 'Delivery Optimization cmdlet is not installed.'
|
||||
}
|
||||
Write-SystemCleanupMessage 'Delivery Optimization: unpinned cache only; size not estimated.'
|
||||
Write-SystemCleanupMessage 'Delivery Optimization: unpinned cache only; size not estimated.' -Detail:$Options.ReportOnly
|
||||
if (-not $Options.ReportOnly) { Delete-DeliveryOptimizationCache -Force -ErrorAction Stop }
|
||||
}
|
||||
Invoke-SystemCleanupCategory $context 'ComponentStore' {
|
||||
Invoke-SystemCleanupProcess $context 'schtasks.exe' '/Run /TN "\Microsoft\Windows\Servicing\StartComponentCleanup"'
|
||||
Write-SystemCleanupMessage 'Component cleanup runs asynchronously under Task Scheduler; completion and reclaimed bytes are not measured here.'
|
||||
Write-SystemCleanupMessage 'Component cleanup runs asynchronously under Task Scheduler; completion and reclaimed bytes are not measured here.' -Detail:$Options.ReportOnly
|
||||
}
|
||||
Invoke-SystemCleanupCategory $context 'DiskCleanup' { Invoke-SystemCleanupDiskCleanup $context }
|
||||
if ($Options.ReportOnly) { Write-SystemCleanupReport $context }
|
||||
@@ -734,4 +757,6 @@ function Invoke-SystemCleanup {
|
||||
}
|
||||
|
||||
# Read inherited caller variables before entering the implementation's local scope.
|
||||
Invoke-SystemCleanup -Options (Get-SystemCleanupOptions)
|
||||
# The function returns structured results for programmatic callers/tests. Do not
|
||||
# let PowerShell auto-format a second, much longer report in the RMM output.
|
||||
Invoke-SystemCleanup -Options (Get-SystemCleanupOptions) | Out-Null
|
||||
|
||||
Reference in New Issue
Block a user