我在 PowerShell 中编写了以下脚本,通过 WinGet 更新应用程序,如果更新失败则强制卸载并重新安装:
<#
.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
当我通过终端手动运行它时,它按预期工作。但是,它不能作为组策略启动脚本工作Computer Configuration\Policies\Windows Settings\Scripts(Startup/Shutdown)
。
我通过将执行策略设置为“允许所有脚本”来打开脚本执行Computer Configuration\Policies\Administrative Templates\Windows Components\Windows Powershell
(这仅用于测试,并未部署给所有用户)。
我一直在测试该脚本是否有效,方法是通过 WinGet 安装旧版本的程序、强制 gpupdate、重新启动计算机,并等待 5 分钟以上以允许脚本异步运行。到目前为止,我还没有成功让脚本运行。
我是否缺少了什么以使该脚本在启动时运行?
事实证明问题不在于组策略,而在于脚本。有两个问题:
WinGet 命令在系统上下文中不可用:https://github.com/microsoft/winget-cli/issues/3049
Powershell WinGet 客户端模块必须在 Powershell 7 中运行,启动脚本默认在 Powershell 5.1 中运行:https://github.com/microsoft/winget-cli/issues/4820
我通过创建一个辅助脚本来下载 Powershell 7,然后使用它启动第一个脚本来解决这个问题:
我还必须更改原始脚本中的所有 WinGet 命令以使用 WinGet 客户端模块: