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
    • 最新
    • 标签
主页 / server / 问题 / 1174342
Accepted
calqium
calqium
Asked: 2025-03-06 01:17:28 +0800 CST2025-03-06 01:17:28 +0800 CST 2025-03-06 01:17:28 +0800 CST

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

  • 772

我在 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 1 个回答
  • 75 Views

1 个回答

  • Voted
  1. Best Answer
    calqium
    2025-03-10T21:01:56+08:002025-03-10T21:01:56+08:00

    事实证明问题不在于组策略,而在于脚本。有两个问题:

    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,然后使用它启动第一个脚本来解决这个问题:

    # Check if Powershell version 7 exists
    $powershellSevenPath = "C:\Program Files\PowerShell\7\pwsh.exe"
    $powershellSeven = Test-Path -Path $powershellSevenPath
    
    if (!$powershellSeven) {
        # If it doesn't exist, install it.
        msiexec.exe /package PowerShell-7.5.0-win-x64.msi /quiet ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ENABLE_PSREMOTING=1 REGISTER_MANIFEST=1 USE_MU=1 ENABLE_MU=1 ADD_PATH=1
    }
    
    # Run script with Powershell 7.
    Start-Process -FilePath $powershellSevenPath -Verb RunAs -ArgumentList ".\Update-WinGet-Applications.ps1"
    

    我还必须更改原始脚本中的所有 WinGet 命令以使用 WinGet 客户端模块:

    <#
    .SYNOPSIS
        Installs the Powershell WinGet client module.
    .DESCRIPTION
        Checks to see if the Powershell WinGet client module is installed. If it isn't, 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 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
            Update-WinGetPackage -Id $appId -Scope Any -Mode Silent -Force
            
            # 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..."
                Uninstall-WinGetPackage -Id $appId  -Scope Any -Mode Silent -Force
                Install-WinGetPackage -Id $appId  -Scope Any -Mode Silent -Force
    
                # 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 {
        # Start logging.
        $logDate = Get-Date -Format FileDateTime
        Start-Transcript -Path C:\Users\$env:USERNAME\Documents\Update-WinGetApplicationsLogs\log-$logDate.txt
    
        # 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."
        } 
    
        # Stop logging.
        Stop-Transcript
    }
    
    
    # Run the script.
    Main
    
    • 0

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果 Windows 服务崩溃,如何自动重新启动它?

  • 无法安排任务(访问被拒绝)

  • 物理机重启时自动重启虚拟机(VMWare)

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