Private/monster_usb.ps1
|
# ============================================================================== # MONSTER SCRIPT ENTERPRISE - USB RESET PAYLOAD (MODULE EDITION) # Файл: Private\monster_usb.ps1 # Назначение: Сброс USB Host-контроллеров с раздельной мультиязычной локализацией # ============================================================================== [CmdletBinding()] param( [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_usb', [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_usb' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'USB' 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 'InitUSB' ` -ComponentName 'monster_usb' ` -Language $Language ` -Context 'USB' # ------------------------------------------------------------------------------ # Загрузка модуля PnpDevice # ------------------------------------------------------------------------------ try { if (-not (Get-Module -Name PnpDevice -ErrorAction SilentlyContinue)) { Import-Module ` -Name PnpDevice ` -ErrorAction Stop } } catch { Write-Log ` -Level 'ERROR' ` -Message 'ModuleError' ` -ComponentName 'monster_usb' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'USB' exit 1 } # ------------------------------------------------------------------------------ # Основная логика сброса USB-контроллеров # ------------------------------------------------------------------------------ try { Write-Log ` -Level 'INFO' ` -Message 'SearchUSB' ` -ComponentName 'monster_usb' ` -Language $Language ` -Context 'USB' $usbControllers = @( Get-PnpDevice ` -Class 'USB' ` -ErrorAction Stop | Where-Object { $_.FriendlyName -like '*Host Controller*' -or $_.FriendlyName -like '*XHCI*' -or $_.FriendlyName -like '*USB Root Hub*' } ) if ($usbControllers.Count -eq 0) { Write-Log ` -Level 'WARNING' ` -Message 'NotFound' ` -ComponentName 'monster_usb' ` -Language $Language ` -Context 'USB' } else { foreach ($controller in $usbControllers) { $devName = if ( -not [string]::IsNullOrWhiteSpace($controller.FriendlyName) ) { $controller.FriendlyName } else { 'Unknown USB Device' } if ($isVerbose) { Write-Log ` -Level 'VERBOSE' ` -Message 'VerboseRestart' ` -ComponentName 'monster_usb' ` -Language $Language ` -Arguments @($devName) ` -Context 'USB' } try { if ( [string]::IsNullOrWhiteSpace( $controller.InstanceId ) ) { throw 'У USB-устройства отсутствует InstanceId.' } Disable-PnpDevice ` -InstanceId $controller.InstanceId ` -Confirm:$false ` -ErrorAction Stop Start-Sleep -Seconds 1 Enable-PnpDevice ` -InstanceId $controller.InstanceId ` -Confirm:$false ` -ErrorAction Stop } catch { Write-Log ` -Level 'ERROR' ` -Message 'DeviceError' ` -ComponentName 'monster_usb' ` -Language $Language ` -Arguments @($devName, $_.Exception.Message) ` -Context 'USB' } } } Write-Log ` -Level 'SUCCESS' ` -Message 'SuccessDone' ` -ComponentName 'monster_usb' ` -Language $Language ` -Context 'USB' exit 0 } catch { Write-Log ` -Level 'CRITICAL' ` -Message 'CritError' ` -ComponentName 'monster_usb' ` -Language $Language ` -Arguments @($_.Exception.Message) ` -Context 'USB' exit 1 } |