Private/monster_optimizer.ps1
|
# ============================================================================== # MONSTER SCRIPT ENTERPRISE - OPTIMIZER PAYLOAD (MODULE EDITION) # Файл: Private\monster_optimizer.ps1 # Назначение: Оптимизация сети и кэша с раздельной мультиязычной локализацией # ============================================================================== [CmdletBinding()] param( [switch]$OptimizeNetwork, [switch]$FlushDNS, [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_optimizer', [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_optimizer' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'OPTIMIZER' exit 1 } } } # ------------------------------------------------------------------------------ # Установка языка # ------------------------------------------------------------------------------ if ( $Language -and (Get-Command -Name Set-MonsterLanguage -ErrorAction SilentlyContinue) ) { Set-MonsterLanguage -Language $Language } # ------------------------------------------------------------------------------ # Основная логика # ------------------------------------------------------------------------------ Write-Log ` -Level 'INFO' ` -Message 'InitOptimizer' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' try { # -------------------------------------------------------------------------- # Сброс DNS-кэша # -------------------------------------------------------------------------- if ($FlushDNS) { Write-Log ` -Level 'INFO' ` -Message 'FlushDnsStart' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' $output = & ipconfig.exe /flushdns 2>&1 $flushExitCode = $LASTEXITCODE if ($flushExitCode -eq 0) { Write-Log ` -Level 'SUCCESS' ` -Message 'FlushDnsSuccess' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' $mainLoc = Get-MonsterLocalization -ComponentName 'monster_optimizer' -Language $Language -Category 'main' $txtOkFlushDns = if ($mainLoc.OkFlushDns) { $mainLoc.OkFlushDns } else { '[OK] Кэш DNS очищен.' } Write-Host $txtOkFlushDns -ForegroundColor Green } else { $mainLoc = Get-MonsterLocalization -ComponentName 'monster_optimizer' -Language $Language -Category 'main' $errFmt = if ($mainLoc.FlushDnsError) { $mainLoc.FlushDnsError } else { 'ipconfig /flushdns exited with error code {0}. Output: {1}' } throw ( $errFmt -f $flushExitCode, ($output -join '; ') ) } } # -------------------------------------------------------------------------- # Оптимизация сетевых параметров # -------------------------------------------------------------------------- if ($OptimizeNetwork) { Write-Log ` -Level 'INFO' ` -Message 'OptNetStart' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' Set-NetIPInterface ` -InterfaceMetric 10 ` -ErrorAction SilentlyContinue Write-Log ` -Level 'SUCCESS' ` -Message 'OptNetSuccess' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' $mainLoc = Get-MonsterLocalization -ComponentName 'monster_optimizer' -Language $Language -Category 'main' $txtOkOptNet = if ($mainLoc.OkOptNet) { $mainLoc.OkOptNet } else { '[OK] Сетевые параметры оптимизированы.' } Write-Host $txtOkOptNet -ForegroundColor Green } # -------------------------------------------------------------------------- # Отсутствие параметров # -------------------------------------------------------------------------- if (-not $OptimizeNetwork -and -not $FlushDNS) { Write-Log ` -Level 'WARNING' ` -Message 'NoParamsWarn' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Context 'OPTIMIZER' $mainLoc = Get-MonsterLocalization -ComponentName 'monster_optimizer' -Language $Language -Category 'main' $txtUsage = if ($mainLoc.UsageInfo) { $mainLoc.UsageInfo } else { 'Использование: monster_optimizer.ps1 [-OptimizeNetwork] [-FlushDNS]' } Write-Host $txtUsage -ForegroundColor Yellow } exit 0 } catch { Write-Log ` -Level 'CRITICAL' ` -Message 'CritError' ` -ComponentName 'monster_optimizer' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'OPTIMIZER' exit 1 } |