# ========================================================= # MAX Windows Toolkit - 使用 Winget 检查更新软件 # 中文界面,统一风格 # ========================================================= $Common = "C:\Setup\windows\lib\common.ps1" if (Test-Path $Common) { . $Common } Write-Header "使用 Winget 检查更新软件" Show-AdminWarn # 检查 Winget 是否可用 function Test-WingetAvailable { $null -ne (Get-Command winget.exe -ErrorAction SilentlyContinue) } # 显示 Winget 未找到帮助信息 function Show-WingetNotFoundHelp { Write-Warn "系统中未找到 winget" Write-Info "" Write-Info "可能的原因:" Write-Info "1. 未安装应用安装程序 (App Installer)" Write-Info "2. Microsoft Store 不可用" Write-Info "3. Windows 版本过旧" Write-Info "4. winget 路径未加载" Write-Info "" Write-Info "可从 Microsoft Store 安装或更新 App Installer" Write-Info "" $Choice = Read-Host "打开 Microsoft Store App Installer 页面?输入 YES 确认" if ($Choice -eq "YES") { Start-Process "ms-windows-store://pdp/?ProductId=9NBLGGH4NNS1" } } # 显示 Winget 版本 function Show-WingetVersion { Write-Section "Winget 版本" try { winget --version } catch { Write-Err "获取 winget 版本失败" Write-Muted $_.Exception.Message } } # 显示 Winget 源 function Show-WingetSource { Write-Section "Winget 源列表" try { winget source list } catch { Write-Err "列出 winget 源失败" Write-Muted $_.Exception.Message } } # 重置 Winget 源 function Reset-WingetSource { Write-Warn "此操作将重置 winget 源" Write-Warn "可能修复源错误或更新元数据问题" Write-Info "" $Confirm = Read-Host "是否继续?输入 YES 确认" if ($Confirm -ne "YES") { Write-Warn "已取消" return } try { winget source reset --force winget source update Write-OK "Winget 源重置完成" } catch { Write-Err "重置 winget 源失败" Write-Muted $_.Exception.Message } } # 更新 Winget 源 function Update-WingetSource { Write-Info "正在更新 winget 源..." try { winget source update Write-OK "Winget 源更新完成" } catch { Write-Err "更新 winget 源失败" Write-Muted $_.Exception.Message } } # 显示可更新软件列表 function Show-UpgradeableSoftware { Write-Section "检查可更新的软件" try { winget upgrade } catch { Write-Err "检查软件更新失败" Write-Muted $_.Exception.Message } } # 升级选定的软件 function Upgrade-SelectedSoftware { Write-Section "可更新的软件列表" try { winget upgrade } catch { Write-Err "检查软件更新失败" return } Write-Info "" Write-Info "请输入要升级的程序包 ID" Write-Info "例如: Google.Chrome" Write-Info "" $PackageId = Read-Host "程序包 ID" if ([string]::IsNullOrWhiteSpace($PackageId)) { Write-Err "程序包 ID 不能为空" return } Write-Info "正在升级: $PackageId" try { winget upgrade --id $PackageId --silent --accept-package-agreements --accept-source-agreements Write-OK "升级命令已完成: $PackageId" } catch { Write-Err "升级失败: $PackageId" Write-Muted $_.Exception.Message } } # 升级所有软件 function Upgrade-AllSoftware { Write-Warn "此操作将升级 winget 检测到的所有程序包" Write-Warn "部分安装程序仍可能显示窗口、需要确认或请求重启" Write-Info "" $Confirm = Read-Host "是否继续?输入 YES 确认" if ($Confirm -ne "YES") { Write-Warn "已取消" return } Write-Info "正在执行 winget upgrade --all..." try { winget upgrade --all --silent --accept-package-agreements --accept-source-agreements Write-OK "Winget 一键升级完成" } catch { Write-Err "Winget 一键升级失败" Write-Muted $_.Exception.Message } } # 导出 Winget 程序包列表 function Export-WingetPackageList { $ExportDir = "C:\Setup\Winget" $ExportFile = Join-Path $ExportDir "winget-packages-$(Get-Date -Format 'yyyyMMdd-HHmmss').json" New-Item -ItemType Directory -Path $ExportDir -Force | Out-Null Write-Info "正在导出 winget 程序包列表..." Write-Info "输出文件: $ExportFile" try { winget export -o $ExportFile --accept-source-agreements Write-OK "导出完成" } catch { Write-Err "导出失败" Write-Muted $_.Exception.Message } } # 显示已安装的 Winget 程序包 function Show-InstalledWingetPackages { Write-Section "Winget 检测到的已安装程序包" try { winget list } catch { Write-Err "列出已安装程序包失败" Write-Muted $_.Exception.Message } } # 搜索程序包 function Search-WingetPackage { $Keyword = Read-Host "输入程序包关键词(例如 chrome、7zip、vscode)" if ([string]::IsNullOrWhiteSpace($Keyword)) { Write-Err "关键词不能为空" return } try { winget search $Keyword } catch { Write-Err "搜索失败" Write-Muted $_.Exception.Message } } # 主入口:检查 Winget 是否可用 if (-not (Test-WingetAvailable)) { Show-WingetNotFoundHelp Pause-Max exit 1 } # 主菜单循环 :wingetLoop while ($true) { Clear-Host Write-Header "使用 Winget 检查更新软件" Write-Info "1. 显示 Winget 版本" Write-Info "2. 显示 Winget 源" Write-Info "3. 更新 Winget 源" Write-Info "4. 重置 Winget 源" Write-Info "5. 检查可更新软件" Write-Info "6. 按程序包 ID 升级指定软件" Write-Info "7. 一键升级所有软件" Write-Info "8. 显示已安装的 Winget 程序包" Write-Info "9. 搜索程序包" Write-Info "10. 导出 Winget 程序包列表" Write-Info "0. 退出" Write-Info "" $Choice = Read-Host "请选择" Write-Line switch ($Choice) { "1" { Show-WingetVersion; Pause-Max } "2" { Show-WingetSource; Pause-Max } "3" { Update-WingetSource; Pause-Max } "4" { Reset-WingetSource; Pause-Max } "5" { Show-UpgradeableSoftware; Pause-Max } "6" { Upgrade-SelectedSoftware; Pause-Max } "7" { Upgrade-AllSoftware; Pause-Max } "8" { Show-InstalledWingetPackages; Pause-Max } "9" { Search-WingetPackage; Pause-Max } "10" { Export-WingetPackageList; Pause-Max } "0" { break :wingetLoop } default { Write-Err "无效选项"; Pause-Max } } }