[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] param( [string]$Path, [ValidateRange(0, 3650)] [int]$Days = 30, [switch]$Apply ) $ErrorActionPreference = 'Stop' $allowedRoot = Join-Path $env:LOCALAPPDATA 'CrashDumps' if (-not $Path) { $Path = $allowedRoot } if (-not (Test-Path -LiteralPath $Path -PathType Container)) { Write-Host "Каталог не найден: $Path" return } $resolved = (Resolve-Path -LiteralPath $Path).ProviderPath.TrimEnd('\') $allowed = (Resolve-Path -LiteralPath $allowedRoot).ProviderPath.TrimEnd('\') if (-not ($resolved -eq $allowed -or $resolved.StartsWith($allowed + '\', [System.StringComparison]::OrdinalIgnoreCase))) { throw "Скрипт работает только внутри $allowed" } $cutoff = (Get-Date).AddDays(-$Days) $files = @( Get-ChildItem -LiteralPath $resolved -File -Filter '*.dmp' -Force -ErrorAction SilentlyContinue | Where-Object { $_.LastWriteTime -lt $cutoff } ) $bytes = ($files | Measure-Object -Property Length -Sum).Sum if ($null -eq $bytes) { $bytes = 0 } Write-Host ("Дампов старше {0} дней: {1}, объём: {2:N1} MB" -f $Days, $files.Count, ($bytes / 1MB)) $files | Select-Object Name, Length, LastWriteTime, FullName | Format-Table -AutoSize if (-not $Apply) { Write-Host 'Предпросмотр. Для удаления добавьте -Apply.' return } $removed = 0 $failed = 0 foreach ($file in $files) { if ($PSCmdlet.ShouldProcess($file.FullName, 'Удалить дамп падения')) { try { Remove-Item -LiteralPath $file.FullName -Force -ErrorAction Stop $removed++ } catch { $failed++ Write-Warning "Не удалось удалить $($file.FullName): $($_.Exception.Message)" } } } Write-Host "Удалено: $removed, ошибок: $failed"