|
| 1 | +[CmdletBinding()] |
| 2 | +param( |
| 3 | + [Parameter(Mandatory=$true)] |
| 4 | + [ValidateSet('install','update','remove','status','list')] |
| 5 | + [string]$Action, |
| 6 | + |
| 7 | + [Parameter(Mandatory=$true)] |
| 8 | + [ValidateSet('microsoft','apple')] |
| 9 | + [string]$Vendor, |
| 10 | + |
| 11 | + [string]$Package, |
| 12 | + [switch]$AllUsers |
| 13 | +) |
| 14 | + |
| 15 | +Set-StrictMode -Version Latest |
| 16 | +$ErrorActionPreference = 'Stop' |
| 17 | + |
| 18 | +function Assert-Winget { |
| 19 | + if (-not (Get-Command winget -ErrorAction SilentlyContinue)) { |
| 20 | + throw 'winget is required for Windows package operations.' |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function Invoke-Winget([string[]]$Args) { |
| 25 | + Assert-Winget |
| 26 | + & winget @Args |
| 27 | + if ($LASTEXITCODE -ne 0) { throw "winget failed with exit code $LASTEXITCODE" } |
| 28 | +} |
| 29 | + |
| 30 | +$ids = @{ |
| 31 | + microsoft = @{ |
| 32 | + 'edge' = 'Microsoft.Edge' |
| 33 | + 'vscode' = 'Microsoft.VisualStudioCode' |
| 34 | + 'powershell' = 'Microsoft.PowerShell' |
| 35 | + 'dotnet' = 'Microsoft.DotNet.SDK.8' |
| 36 | + 'git' = 'Git.Git' |
| 37 | + } |
| 38 | + apple = @{ |
| 39 | + 'icloud' = 'Apple.iCloud' |
| 40 | + 'applemusic' = 'Apple.AppleMusic' |
| 41 | + 'devices' = 'Apple.AppleDevices' |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +if ($Action -eq 'list') { |
| 46 | + $ids[$Vendor].GetEnumerator() | Sort-Object Name | Format-Table -AutoSize |
| 47 | + exit 0 |
| 48 | +} |
| 49 | + |
| 50 | +if (-not $Package) { |
| 51 | + throw 'Specify -Package, or use -Action list to view supported package aliases.' |
| 52 | +} |
| 53 | + |
| 54 | +if (-not $ids[$Vendor].ContainsKey($Package)) { |
| 55 | + throw "Unsupported $Vendor package alias '$Package'. Use -Action list." |
| 56 | +} |
| 57 | + |
| 58 | +$id = $ids[$Vendor][$Package] |
| 59 | +$common = @('--id', $id, '--exact', '--source', 'winget') |
| 60 | +if ($AllUsers) { $common += '--scope'; $common += 'machine' } |
| 61 | + |
| 62 | +switch ($Action) { |
| 63 | + 'install' { Invoke-Winget (@('install') + $common + @('--accept-package-agreements','--accept-source-agreements')) } |
| 64 | + 'update' { Invoke-Winget (@('upgrade') + $common + @('--accept-package-agreements','--accept-source-agreements')) } |
| 65 | + 'remove' { Invoke-Winget (@('uninstall') + $common) } |
| 66 | + 'status' { Invoke-Winget (@('list') + $common) } |
| 67 | +} |
0 commit comments