improve detection of space savings
This commit is contained in:
+73
-1
@@ -7,6 +7,7 @@ Reads Cleanup* variables from the calling scope (including the RMM Run-Script
|
||||
dot-source scope). Missing variables use Get-SystemCleanupDefaults. No param()
|
||||
block deliberately: RMM variables must not be overwritten by parameter defaults.
|
||||
ReportOnly defaults to true and assesses enabled and disabled categories separately.
|
||||
It also reports hibernation/VSS usage and notes potential component-store savings without running DISM analysis.
|
||||
Server exclusions still apply. See rmm/Start-SystemCleanup.ps1 and
|
||||
docs/SystemCleanup.md for options, server exclusions, and legacy operation mapping.
|
||||
The core is self-contained; loading Tools.ps1 is the RMM shim's responsibility.
|
||||
@@ -315,11 +316,79 @@ function Get-SystemCleanupReportTotals {
|
||||
}
|
||||
}
|
||||
|
||||
function Get-SystemCleanupVolumeId {
|
||||
param($Reference)
|
||||
# CIM references can be embedded instances or escaped WMI object-path strings.
|
||||
$text = if ($Reference -is [string]) { $Reference } elseif ($Reference.DeviceID) { [string]$Reference.DeviceID } else { [string]$Reference }
|
||||
$match = [regex]::Match($text, '(?i)Volume\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}')
|
||||
if (-not $match.Success) { throw 'Cannot identify the volume in a shadow-storage reference.' }
|
||||
return $match.Value.ToLowerInvariant()
|
||||
}
|
||||
|
||||
function Get-SystemCleanupShadowStorageDiagnostic {
|
||||
$volumes = @(Get-CimInstance -ClassName Win32_Volume -ErrorAction Stop)
|
||||
$systemVolume = @($volumes | Where-Object { $_.DriveLetter -eq $env:SystemDrive })
|
||||
if ($systemVolume.Count -ne 1) { throw 'Cannot uniquely identify the system volume.' }
|
||||
$systemId = Get-SystemCleanupVolumeId $systemVolume[0]
|
||||
$storage = @(Get-CimInstance -ClassName Win32_ShadowStorage -ErrorAction Stop | Where-Object {
|
||||
(Get-SystemCleanupVolumeId $_.Volume) -eq $systemId
|
||||
})
|
||||
if (-not $storage.Count) { return "for $env:SystemDrive - no shadow-storage association" }
|
||||
$used = [decimal]0; $allocated = [decimal]0; $locations = @()
|
||||
foreach ($item in $storage) {
|
||||
# A missing measurement must not be converted to zero.
|
||||
foreach ($property in @('UsedSpace', 'AllocatedSpace')) {
|
||||
$bytes = [uint64]0
|
||||
if (-not [uint64]::TryParse([string]$item.$property, [ref]$bytes)) { throw "Invalid shadow-storage $property." }
|
||||
if ($property -eq 'UsedSpace') { $used += $bytes } else { $allocated += $bytes }
|
||||
}
|
||||
$targetId = Get-SystemCleanupVolumeId $item.DiffVolume
|
||||
$target = @($volumes | Where-Object { (Get-SystemCleanupVolumeId $_) -eq $targetId })
|
||||
$locations += if ($target.Count -eq 1 -and $target[0].DriveLetter) { $target[0].DriveLetter } else { 'unmounted/unknown volume' }
|
||||
}
|
||||
return ('for {0} - {1:N2} GiB used / {2:N2} GiB allocated; stored on {3}' -f $env:SystemDrive, ($used / 1GB), ($allocated / 1GB), (($locations | Select-Object -Unique) -join ', '))
|
||||
}
|
||||
|
||||
function Get-SystemCleanupDiagnostics {
|
||||
param($Context)
|
||||
$diagnostics = [ordered]@{}
|
||||
if (-not $Context.Options.ReportOnly) { return $diagnostics }
|
||||
if ($Context.OS.IsServer) {
|
||||
$diagnostics.Hibernation = 'not assessed (server policy)'
|
||||
$diagnostics.ShadowStorage = 'not assessed (server policy)'
|
||||
} else {
|
||||
try {
|
||||
$path = Join-Path $env:SystemDrive 'hiberfil.sys'
|
||||
$file = Get-Item -LiteralPath $path -Force -ErrorAction Stop
|
||||
if ($file.PSIsContainer -or ($file.Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw 'Unexpected hibernation file type.' }
|
||||
$diagnostics.Hibernation = '{0}: {1:N2} GiB file size' -f $path, ($file.Length / 1GB)
|
||||
} catch [System.Management.Automation.ItemNotFoundException] {
|
||||
$diagnostics.Hibernation = 'hiberfil.sys not present'
|
||||
} catch {
|
||||
Write-SystemCleanupMessage "Hibernation diagnostic: $($_.Exception.Message)" -Detail
|
||||
$diagnostics.Hibernation = 'unavailable (file inspection failed; see verbose)'
|
||||
}
|
||||
try { $diagnostics.ShadowStorage = Get-SystemCleanupShadowStorageDiagnostic } catch {
|
||||
Write-SystemCleanupMessage "Shadow-storage diagnostic: $($_.Exception.Message)" -Detail
|
||||
$diagnostics.ShadowStorage = 'unavailable (storage inspection failed; see verbose)'
|
||||
}
|
||||
}
|
||||
# Avoid slow DISM analysis during previews; potential savings are not measured.
|
||||
$diagnostics.ComponentStore = 'not analyzed; removing superseded updates may free substantial space, or little/none.'
|
||||
return $diagnostics
|
||||
}
|
||||
|
||||
function Write-SystemCleanupReport {
|
||||
param($Context)
|
||||
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)
|
||||
if ($Context.ContainsKey('Diagnostics') -and $Context.Diagnostics.Count) {
|
||||
Write-SystemCleanupMessage 'DIAGNOSTICS - current usage/potential, excluded from savings totals'
|
||||
foreach ($entry in $Context.Diagnostics.GetEnumerator()) {
|
||||
Write-SystemCleanupMessage (" {0}: {1}" -f $entry.Key, $entry.Value)
|
||||
}
|
||||
}
|
||||
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')) {
|
||||
@@ -747,7 +816,10 @@ function Invoke-SystemCleanup {
|
||||
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 }
|
||||
if ($Options.ReportOnly) {
|
||||
$context.Diagnostics = Get-SystemCleanupDiagnostics $context
|
||||
Write-SystemCleanupReport $context
|
||||
}
|
||||
if ($context.Errors) { throw "$($context.Errors) cleanup category/maintenance error(s). Review the category messages above." }
|
||||
return @($context.Stats.Values | Sort-Object @{Expression={ if ($_.Status -eq 'Blocked on server') { 2 } elseif ($_.Enabled) { 0 } else { 1 } }}, Category)
|
||||
} finally {
|
||||
|
||||
Reference in New Issue
Block a user