# ========================================================= # MAX Windows Toolkit - 公共函数库 # 统一风格:中文输出、彩色框线、一致格式 # ========================================================= # 设置 UTF-8 输出,避免中文乱码 try { chcp 65001 | Out-Null [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 } catch {} # 全局配置 if (-not $Global:LocalRoot) { $Global:LocalRoot = "C:\Setup\windows" } if (-not $Global:LogDir) { $Global:LogDir = "C:\Setup\Logs" } if (-not $Global:RepoBase) { $Global:RepoBase = "https://dl.unvmax.com/windows" } $Global:AppBaseUrl = "$Global:RepoBase/apps" $Global:WindowsBaseUrl = $Global:RepoBase New-Item -ItemType Directory -Path $Global:LocalRoot -Force -ErrorAction SilentlyContinue | Out-Null New-Item -ItemType Directory -Path $Global:LogDir -Force -ErrorAction SilentlyContinue | Out-Null # ========================================================= # 输出函数 # ========================================================= # 标准标题栏 function Write-Header { param([string]$Title) Write-Host "┌─────────────────────────────────────────────┐" -ForegroundColor Cyan Write-Host "│ $Title" -ForegroundColor Green Write-Host "│ 源站: $Global:RepoBase" -ForegroundColor DarkGray Write-Host "│ 本地: $Global:LocalRoot" -ForegroundColor DarkGray Write-Host "└─────────────────────────────────────────────┘" -ForegroundColor Cyan Write-Host "" } # 分节标题 function Write-Section { param([string]$Text) Write-Host "" Write-Host "── $Text ──" -ForegroundColor Magenta } # 输出颜色化信息 function Write-OK { Write-Host " ✔ $($args[0])" -ForegroundColor Green } function Write-Warn { Write-Host " ⚠ $($args[0])" -ForegroundColor Yellow } function Write-Err { Write-Host " ✘ $($args[0])" -ForegroundColor Red } function Write-Info { Write-Host " $($args[0])" -ForegroundColor White } function Write-Muted { Write-Host " $($args[0])" -ForegroundColor DarkGray } # 等待按键返回 function Pause-Max { Write-Host "" Write-Host "按 Enter 返回..." -ForegroundColor DarkGray Read-Host | Out-Null } # 分隔线 function Write-Line { Write-Host "──────────────────────────────────────" -ForegroundColor DarkGray } # ========================================================= # 管理函数 # ========================================================= # 检查管理员权限 function Test-IsAdmin { $CurrentIdentity = [Security.Principal.WindowsIdentity]::GetCurrent() $Principal = New-Object Security.Principal.WindowsPrincipal($CurrentIdentity) return $Principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } # 管理员警告 function Show-AdminWarn { if (-not (Test-IsAdmin)) { Write-Warn "未以管理员身份运行,部分功能可能失败" Write-Warn "建议关闭窗口,右键「以管理员身份运行」PowerShell" } } # 下载文件 function Download-File { param([string]$Url, [string]$OutFile) $Dir = Split-Path $OutFile -Parent New-Item -ItemType Directory -Path $Dir -Force -ErrorAction SilentlyContinue | Out-Null try { Write-Host " 下载中: $Url" -ForegroundColor Cyan Invoke-WebRequest -Uri $Url -OutFile $OutFile -UseBasicParsing -ErrorAction Stop if (Test-Path $OutFile) { $Size = (Get-Item $OutFile).Length $SizeStr = if ($Size -gt 1MB) { "$([Math]::Round($Size/1MB,2)) MB" } else { "$([Math]::Round($Size/1KB,2)) KB" } Write-OK "下载完成: $SizeStr" return $true } Write-Err "下载失败: 文件未找到" return $false } catch { Write-Err "下载失败: $Url" Write-Muted $_.Exception.Message return $false } } # 关闭进程 function Kill-Processes { param([string[]]$ProcessNames) foreach ($Name in $ProcessNames) { if ([string]::IsNullOrWhiteSpace($Name)) { continue } Get-Process -Name $Name -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue } } # 重启资源管理器 function Restart-Explorer { Write-Host "" Write-Host " 重启资源管理器..." -ForegroundColor Cyan Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Start-Process explorer.exe Write-OK "资源管理器已重启" }