Escrevi o seguinte script no PowerShell para atualizar aplicativos por meio do WinGet e forçar a desinstalação e reinstalação caso a atualização falhe:
<#
.SYNOPSIS
Installs the Powershell WinGet client module.
.DESCRIPTION
Checks to see if the Powershell WinGet client module is installed. If not, attempts to install it.
If the install fails, exits the program.
.NOTES
The module can be found here: https://www.powershellgallery.com/packages/Microsoft.WinGet.Client/1.10.320.
#>
function Install-WinGetClientModule {
# Check if official Powershell WinGet client is installed.
if (Get-Module -ListAvailable -Name Microsoft.WinGet.Client) {
Write-Host "Powershell Module for the Windows Package Manager Client is installed."
} else {
# Install NuGet package provider, which is a requirement to install the module.
Write-Host "Installing NuGet..."
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
# Check if the NuGet installed successfully.
if ($?) {
Write-Host "Successfully installed." -ForegroundColor Green
} else {
# If install could not be completed, exit script.
Write-Host "Could not install NuGet." -ForegroundColor Red
exit
}
# Set PSGallery as a trusted repository to install from.
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
# Install the module.
Write-Host "Installing the Powershell Module for the Windows Package Manager Client..."
Install-Module -Name Microsoft.WinGet.Client
# Check if the module installed successfully.
if ($?) {
Write-Host "Successfully installed." -ForegroundColor Green
} else {
# If install could not be completed, exit script.
Write-Host "Could not install the Powershell Module for the Windows Package Manager Client." -ForegroundColor Red
exit
}
}
}
<#
.SYNOPSIS
Get WinGet applications that require updates.
#>
function Get-ApplicationsWithUpdates {
# Store the applications with updates available through WinGet
$applicationsWithUpdates = Get-WinGetPackage | Where-Object IsUpdateAvailable
return $applicationsWithUpdates
}
<#
.SYNOPSIS
Update applications though WinGet.
#>
function Update-Applications {
param (
[Parameter(Mandatory = $true)]
[Array]$applicationsToUpdate
)
# Update each application provided.
foreach ($app in $applicationsToUpdate) {
$appName = $app.Name
$appId = $app.Id
Write-Host "Updating $appName..."
# Update the application
winget upgrade --id $appId --accept-package-agreements --accept-source-agreements
# Check if the application was successfully updated.
if ($?) {
Write-Host "$appName updated to the latest version." -ForegroundColor Green
} else {
# If the update failed, attempt to reinstall.
Write-Host "Failed to update $appName. Attempting to reinstall..."
winget uninstall --id $appId --silent --accept-source-agreements
winget install --id $appId --accept-package-agreements --accept-source-agreements
# Check if the application reinstalled successfully.
if ($?) {
Write-Host "$appName updated to the latest version." -ForegroundColor Green
} else {
Write-Host "Failed to update $appName." -ForegroundColor Red
}
}
}
}
function Main {
# Get Powershell WinGet client.
Install-WinGetClientModule
# Check for application updates through WinGet.
$applicationsToUpdate = Get-ApplicationsWithUpdates
Write-Host $applicationsToUpdate
# Check that the number of applications to update isn't 0.
if ($applicationsToUpdate.Count -gt 0) {
# Update applications.
Update-Applications -ApplicationsToUpdate $applicationsToUpdate
} else {
Write-Host "All applications are up to date."
}
}
# Run the script.
Main
Quando eu o executo manualmente pelo terminal, ele funciona como esperado. No entanto, ele não funciona como um script de inicialização de Política de Grupo em Computer Configuration\Policies\Windows Settings\Scripts(Startup/Shutdown)
.
Ativei a Execução de Script Computer Configuration\Policies\Administrative Templates\Windows Components\Windows Powershell
definindo a Política de Execução como "Permitir todos os scripts" (isso é apenas para teste e não é implantado para todos os usuários).
Tenho testado para ver se o script funciona instalando versões mais antigas de programas via WinGet, forçando gpupdate, reiniciando o computador e esperando mais de 5 minutos para permitir que o script seja executado de forma assíncrona. Até agora, não tive sorte em fazer o script ser executado.
Há algo que esteja faltando para que este script funcione na inicialização?