Private/monster_cleanup.ps1
|
# ============================================================================== # MONSTER SCRIPT ENTERPRISE - CLEANUP PAYLOAD (MODULE EDITION) # Файл: Private\monster_cleanup.ps1 # Назначение: Очистка временных файлов и памяти с раздельной мультиязычной локализацией # ============================================================================== [CmdletBinding()] param( [switch]$TempFiles, [switch]$Memory, [Parameter(Mandatory = $false)] [ValidatePattern('^[a-zA-Z]{2}(-[a-zA-Z]{2,4})?$')] [string]$Language ) # ------------------------------------------------------------------------------ # Автономный fallback для Write-Log # ------------------------------------------------------------------------------ if (-not (Get-Command -Name Write-Log -ErrorAction SilentlyContinue)) { function Write-Log { param( [Parameter(Mandatory = $true)] [string]$Level, [Parameter(Mandatory = $true)] [string]$Message, [string]$Context = 'GENERAL', [string]$ComponentName = 'monster_cleanup', [string]$Language = '', [object[]]$Arguments ) $timestamp = (Get-Date).ToString('yyyy-MM-dd HH:mm:ss') $color = switch ($Level.ToUpperInvariant()) { 'SUCCESS' { 'Green' } 'WARNING' { 'Yellow' } 'ERROR' { 'Red' } 'CRITICAL' { 'DarkRed' } 'VERBOSE' { 'Magenta' } 'DEBUG' { 'DarkGray' } default { 'Cyan' } } Write-Host ` "[$timestamp] [$Level] [$Context] $Message" ` -ForegroundColor $color } } # ------------------------------------------------------------------------------ # Определение директории скрипта # ------------------------------------------------------------------------------ $scriptDir = $PSScriptRoot if (-not $scriptDir -and $MyInvocation.MyCommand.Path) { $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path } if (-not $scriptDir) { $scriptDir = (Get-Location).Path } # ------------------------------------------------------------------------------ # Подключение MonsterCore # ------------------------------------------------------------------------------ $coreModulePath = Join-Path ` -Path (Split-Path -Parent $scriptDir) ` -ChildPath 'Private\MonsterCore.psm1' if (-not (Test-Path -LiteralPath $coreModulePath -PathType Leaf)) { $coreModulePath = Join-Path ` -Path $scriptDir ` -ChildPath 'MonsterCore.psm1' } if (-not (Get-Command -Name Get-MonsterLocalization -ErrorAction SilentlyContinue)) { if (Test-Path -LiteralPath $coreModulePath -PathType Leaf) { try { Import-Module ` -Name $coreModulePath ` -Force ` -ErrorAction Stop } catch { Write-Log ` -Level 'ERROR' ` -Message 'CoreLoadError' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'CLEANUP' exit 1 } } } # ------------------------------------------------------------------------------ # Установка языка # ------------------------------------------------------------------------------ if ( $Language -and (Get-Command -Name Set-MonsterLanguage -ErrorAction SilentlyContinue) ) { Set-MonsterLanguage -Language $Language } $isVerbose = ( $PSBoundParameters.ContainsKey['Verbose'] -or $VerbosePreference -eq 'Continue' ) Write-Log ` -Level 'INFO' ` -Message 'InitCleanup' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' # ------------------------------------------------------------------------------ # Основная логика # ------------------------------------------------------------------------------ try { # -------------------------------------------------------------------------- # Очистка временных файлов # -------------------------------------------------------------------------- if ($TempFiles) { Write-Log ` -Level 'INFO' ` -Message 'TempFilesStart' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' $tempPaths = @( $env:TEMP (Join-Path -Path $env:SystemRoot -ChildPath 'Temp') ) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | Select-Object -Unique $removedCount = 0 $failedCount = 0 foreach ($path in $tempPaths) { if (-not (Test-Path -LiteralPath $path -PathType Container)) { continue } $items = @( Get-ChildItem ` -LiteralPath $path ` -Recurse ` -Force ` -ErrorAction SilentlyContinue ) foreach ($item in $items) { try { Remove-Item ` -LiteralPath $item.FullName ` -Force ` -Recurse ` -ErrorAction Stop $removedCount++ } catch { $failedCount++ if ($isVerbose) { Write-Log ` -Level 'VERBOSE' ` -Message 'VerboseItemFail' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Arguments @($item.FullName, $_.Exception.Message) ` -Context 'CLEANUP' } } } } if ($failedCount -gt 0) { Write-Log ` -Level 'WARNING' ` -Message 'TempFilesPartial' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Arguments @($removedCount, $failedCount) ` -Context 'CLEANUP' } else { Write-Log ` -Level 'SUCCESS' ` -Message 'TempFilesSuccess' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' } $mainLoc = Get-MonsterLocalization -ComponentName 'monster_cleanup' -Language $Language -Category 'main' $txtOkTempFiles = if ($mainLoc.OkTempFiles) { $mainLoc.OkTempFiles } else { '[OK] Временные файлы очищены.' } Write-Host $txtOkTempFiles -ForegroundColor Green } # -------------------------------------------------------------------------- # Освобождение неиспользуемой памяти .NET # -------------------------------------------------------------------------- if ($Memory) { Write-Log ` -Level 'INFO' ` -Message 'MemoryStart' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers() [System.GC]::Collect() Write-Log ` -Level 'SUCCESS' ` -Message 'MemorySuccess' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' $mainLoc = Get-MonsterLocalization -ComponentName 'monster_cleanup' -Language $Language -Category 'main' $txtOkMemory = if ($mainLoc.OkMemory) { $mainLoc.OkMemory } else { '[OK] Память освобождена.' } Write-Host $txtOkMemory -ForegroundColor Green } # -------------------------------------------------------------------------- # Режим без параметров # -------------------------------------------------------------------------- if (-not $TempFiles -and -not $Memory) { Write-Log ` -Level 'INFO' ` -Message 'DefaultCleanup' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Context 'CLEANUP' } exit 0 } catch { Write-Log ` -Level 'CRITICAL' ` -Message 'CritError' ` -ComponentName 'monster_cleanup' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'CLEANUP' exit 1 } |