AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1239420

calqium's questions

Martin Hope
calqium
Asked: 2025-03-06 01:17:28 +0800 CST

通过 WinGet 更新应用程序的脚本未通过 GPO 运行

  • 5

我在 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 分钟以上以允许脚本异步运行。到目前为止,我还没有成功让脚本运行。

我是否缺少了什么以使该脚本在启动时运行?

windows
  • 1 个回答
  • 75 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve