Support user-scoped winget installs on Windows

This commit is contained in:
Fabio Scotto di Santolo
2026-04-02 15:20:04 +02:00
parent 4acbc2c68a
commit 3138e558da
2 changed files with 17 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ windows_winget_packages:
name: Postman name: Postman
- id: Spotify.Spotify - id: Spotify.Spotify
name: Spotify name: Spotify
scope: user
- id: Telegram.TelegramDesktop - id: Telegram.TelegramDesktop
name: Telegram Desktop name: Telegram Desktop

View File

@@ -124,13 +124,28 @@
script: | script: |
$packageId = '{{ item.id }}' $packageId = '{{ item.id }}'
$packageName = '{{ item.name | default(item.id) }}' $packageName = '{{ item.name | default(item.id) }}'
$packageScope = '{{ item.scope | default('' ) }}'
$installArgs = @(
'install'
'--id', $packageId
'--exact'
'--silent'
'--accept-package-agreements'
'--accept-source-agreements'
'--disable-interactivity'
)
if (-not [string]::IsNullOrWhiteSpace($packageScope)) {
$installArgs += @('--scope', $packageScope)
}
$installed = & winget list --id $packageId --exact --accept-source-agreements --disable-interactivity 2>$null $installed = & winget list --id $packageId --exact --accept-source-agreements --disable-interactivity 2>$null
if ($LASTEXITCODE -eq 0 -and $installed -match [regex]::Escape($packageId)) { if ($LASTEXITCODE -eq 0 -and $installed -match [regex]::Escape($packageId)) {
$Ansible.Changed = $false $Ansible.Changed = $false
return return
} }
& winget install --id $packageId --exact --silent --accept-package-agreements --accept-source-agreements --disable-interactivity & winget @installArgs
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
throw "Failed to install $packageName with winget" throw "Failed to install $packageName with winget"
} }