如何让Windows更新PowerShell脚本自动接受NuGet安装?
解决NuGet安装自动确认问题的PowerShell脚本优化
问题描述
我拥有一个用于强制执行Windows更新的PowerShell脚本,尝试了多种命令以实现NuGet安装的自动确认,但运行脚本时始终弹出NuGet安装的授权提示(见附图),现寻求让脚本自动接受NuGet安装的解决方案。
原脚本
param ( [switch]$ResetDistributionFolder, [switch]$ListOnly, [switch]$MicrosoftUpdate, [switch]$NoAutoReboot, [switch]$RebootAtMidnight, [switch]$SkipModuleUpdate, [switch]$SkipDrivers, [switch]$SkipFirmware ) if ($ResetDistributionFolder) { Write-Host 'Stopping services...' Stop-Service 'Windows Update' -Force Stop-Service cryptSvc -Force Stop-Service DoSvc -Force Stop-Service bits -Force Stop-Service msiserver -Force Write-Host 'Removing SoftwareDistribution folder...' Remove-Item 'C:\Windows\SoftwareDistribution\' -Force -Recurse Write-Host 'Removing CatRoot2 folder...' Remove-Item 'C:\Windows\System32\catroot2\' -Force -Recurse Write-Host 'Starting services...' Start-Service cryptSvc Start-Service 'Windows Update' } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 if (-not (Get-PackageProvider -Name 'NuGet')) { Write-Host 'Installing NuGet...' Install-PackageProvider -Name NuGet -Confirm:$false } if (-not (Get-InstalledModule -Name 'PSWindowsUpdate')) { Write-Host 'Installing Windows Update PS module...' Install-Module PSWindowsUpdate -MinimumVersion 2.2.0.2 -Force } else { if (!$SkipModuleUpdate) { Write-Host 'Updating Windows Update PS module...' Update-Module -Name PSWindowsUpdate -Force } } Write-Host 'Importing Windows Update PS module...' Import-Module PSWindowsUpdate -Force $WUCommand = 'Get-WindowsUpdate' if (!$ListOnly) { $WUCommand = $WUCommand + ' -Install -AcceptAll' if ($RebootAtMidnight -and !$NoAutoReboot) { Write-Host 'Reboot at Midnight selected, setting Auto-Reboot on Finish to False.' $NoAutoReboot = $true Exit } } else { if ($RebootAtMidnight) { Write-Host 'List Only selected. No reboots will be performed.' $RebootAtMidnight = $false Exit } } if ($MicrosoftUpdate) { Write-Host 'We will be checking with Microsofts update servers directly.' $WUCommand = $WUCommand + ' -MicrosoftUpdate' } else { Write-Host 'We will be checking with the update server indicated in Group Policies (ie: WSUS).' } if ($SkipDrivers) { Write-Host 'We will not be including Drivers.' $WUCommand = $WUCommand + ' -NotCategory "Drivers"' } else { Write-Host 'We will be including Drivers.' if ($SkipFirmware) { Write-Host 'We will not be including Firmware updates.' $WUCommand = $WUCommand + ' -NotTitle "Firmware"' } else { Write-Host 'We will be including Firmware updates.' } } Write-Host 'Starting Windows Update Check/Install...' Write-Host ('-> Command used: "{0}"' -f $WUCommand) Invoke-Expression $WUCommand
解决方案
弹出的提示是确认是否信任NuGet包源,仅用-Confirm:$false无法跳过这个验证,需要通过以下修改解决:
核心修改点
- 提前配置PowerShellGallery为受信任源:避免后续模块安装时的信任提示
- 增强NuGet安装命令:添加
-Force参数跳过信任确认,可选-Scope CurrentUser避免管理员权限要求
修改后的完整脚本
param ( [switch]$ResetDistributionFolder, [switch]$ListOnly, [switch]$MicrosoftUpdate, [switch]$NoAutoReboot, [switch]$RebootAtMidnight, [switch]$SkipModuleUpdate, [switch]$SkipDrivers, [switch]$SkipFirmware ) if ($ResetDistributionFolder) { Write-Host 'Stopping services...' Stop-Service 'Windows Update' -Force Stop-Service cryptSvc -Force Stop-Service DoSvc -Force Stop-Service bits -Force Stop-Service msiserver -Force Write-Host 'Removing SoftwareDistribution folder...' Remove-Item 'C:\Windows\SoftwareDistribution\' -Force -Recurse Write-Host 'Removing CatRoot2 folder...' Remove-Item 'C:\Windows\System32\catroot2\' -Force -Recurse Write-Host 'Starting services...' Start-Service cryptSvc Start-Service 'Windows Update' } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # 设置PowerShellGallery为受信任包源 if (-not (Get-PSRepository -Name PSGallery -ErrorAction SilentlyContinue)) { Register-PSRepository -Name PSGallery -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted } else { Set-PSRepository -Name PSGallery -InstallationPolicy Trusted } if (-not (Get-PackageProvider -Name 'NuGet')) { Write-Host 'Installing NuGet...' Install-PackageProvider -Name NuGet -Force -Confirm:$false -Scope CurrentUser } if (-not (Get-InstalledModule -Name 'PSWindowsUpdate')) { Write-Host 'Installing Windows Update PS module...' Install-Module PSWindowsUpdate -MinimumVersion 2.2.0.2 -Force -Scope CurrentUser } else { if (!$SkipModuleUpdate) { Write-Host 'Updating Windows Update PS module...' Update-Module -Name PSWindowsUpdate -Force -Scope CurrentUser } } Write-Host 'Importing Windows Update PS module...' Import-Module PSWindowsUpdate -Force $WUCommand = 'Get-WindowsUpdate' if (!$ListOnly) { $WUCommand = $WUCommand + ' -Install -AcceptAll' if ($RebootAtMidnight -and !$NoAutoReboot) { Write-Host 'Reboot at Midnight selected, setting Auto-Reboot on Finish to False.' $NoAutoReboot = $true Exit } } else { if ($RebootAtMidnight) { Write-Host 'List Only selected. No reboots will be performed.' $RebootAtMidnight = $false Exit } } if ($MicrosoftUpdate) { Write-Host 'We will be checking with Microsofts update servers directly.' $WUCommand = $WUCommand + ' -MicrosoftUpdate' } else { Write-Host 'We will be checking with the update server indicated in Group Policies (ie: WSUS).' } if ($SkipDrivers) { Write-Host 'We will not be including Drivers.' $WUCommand = $WUCommand + ' -NotCategory "Drivers"' } else { Write-Host 'We will be including Drivers.' if ($SkipFirmware) { Write-Host 'We will not be including Firmware updates.' $WUCommand = $WUCommand + ' -NotTitle "Firmware"' } else { Write-Host 'We will be including Firmware updates.' } } Write-Host 'Starting Windows Update Check/Install...' Write-Host ('-> Command used: "{0}"' -f $WUCommand) Invoke-Expression $WUCommand
关键说明
-Scope CurrentUser:将包和模块安装到当前用户目录,无需管理员权限,适合非特权账号运行-Force:强制跳过所有交互式提示,包括包源信任确认- 配置PSGallery为受信任源:一次性配置后,后续运行脚本不会再弹出信任提示
内容的提问来源于stack exchange,提问作者André Tavares
相关产品推荐
相关产品推荐


