使用PowerShell升级Win10/11到22H2功能更新遇数组越界错误求助
部署Windows 22H2功能更新PowerShell脚本报错排查
问题描述
我需要通过PowerShell脚本给安装了RMM代理的Win10/Win11设备部署22H2功能更新,但执行脚本时触发以下错误:
Index was outside the bounds of the array. At line:42 char:5 + $downloader.Updates = $featureUpdate + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException + FullyQualifiedErrorId : System.IndexOutOfRangeException Exception from HRESULT: 0x80240004
脚本已能正确识别到"Feature update to Windows 10, version 22H2"更新对象,但仍报错。原脚本代码如下:
# Check if running on Windows 10 or Windows 11 $osVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version if (-not ($osVersion -match "^10\." -or $osVersion -match "^11\.")) { Write-Host "No Windows 10 or Windows 11 detected! Exiting..." exit } # Check if Windows Update service is running $wuaService = Get-Service -Name wuauserv if ($wuaService.Status -ne "Running") { Write-Host "Windows Update service is not running. Please start the service and try again." exit } # Check if System Restore is enabled if ((Get-ComputerRestorePoint).Count -eq 0) { Write-Host "System Restore is not enabled on this system. Please enable it and try again." exit } # Create a system restore point $timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss" $restorePointDescription = "Restorepoint - $timestamp" Checkpoint-Computer -Description $restorePointDescription -RestorePointType "MODIFY_SETTINGS" # Create a Windows Update session $wuaSession = New-Object -ComObject Microsoft.Update.Session # Search for available updates $searcher = $wuaSession.CreateUpdateSearcher() $updates = $searcher.Search("IsInstalled=0 and DeploymentAction=*") # Filter updates to find the feature update $featureUpdate = $updates.Updates | Where-Object { $_.Title -like ("*Feature Update*") } # Check if a feature update is available if ($featureUpdate) { # Download and install the feature update $downloader = $wuaSession.CreateUpdateDownloader() $downloader.Updates = $featureUpdate $downloader.Download() $installer = $wuaSession.CreateUpdateInstaller() $installer.Updates = $featureUpdate $installResult = $installer.Install() # Check the installation result if ($installResult.ResultCode -eq "2") { Write-Host "Feature update installed successfully. Please restart" Start-Sleep -Second 10 exit } else { Write-Host "Feature update installation failed with error code $($installResult.ResultCode)." } } else { Write-Host "No feature update is available." }
错误原因
$downloader.Updates和$installer.Updates要求接收**Microsoft.Update.UpdateCollection类型**的对象,而当Where-Object只筛选出单个更新时,PowerShell会将其解析为单个Update对象(而非集合),直接赋值就会触发索引越界错误;同时HRESULT 0x80240004对应WU_E_INVALID_ARG,即传入的参数类型无效。
修复方案
修改脚本中筛选更新和赋值的逻辑,先创建UpdateCollection对象,再将筛选到的更新添加到集合中,确保参数类型正确。同时优化筛选条件,精准匹配22H2更新:
修改后的关键代码片段
# Create a Windows Update session $wuaSession = New-Object -ComObject Microsoft.Update.Session # Search for available updates $searcher = $wuaSession.CreateUpdateSearcher() $updates = $searcher.Search("IsInstalled=0 and DeploymentAction=*") # 创建UpdateCollection对象用于存储筛选后的更新 $featureUpdates = New-Object -ComObject Microsoft.Update.UpdateCollection # 精准筛选22H2功能更新并添加到集合 $updates.Updates | Where-Object { $_.Title -like "*Feature Update*" -and $_.Title -like "*22H2*" } | ForEach-Object { $featureUpdates.Add($_) | Out-Null # 忽略Add方法的返回值 } # Check if a feature update is available if ($featureUpdates.Count -gt 0) { # Download and install the feature update $downloader = $wuaSession.CreateUpdateDownloader() $downloader.Updates = $featureUpdates $downloader.Download() $installer = $wuaSession.CreateUpdateInstaller() $installer.Updates = $featureUpdates $installResult = $installer.Install() # Check the installation result if ($installResult.ResultCode -eq 2) { # 直接用数字而非字符串更可靠 Write-Host "Feature update installed successfully. Please restart" Start-Sleep -Seconds 10 # 修正原脚本的拼写错误 exit } else { Write-Host "Feature update installation failed with error code $($installResult.ResultCode)." } } else { Write-Host "No 22H2 feature update is available." }
额外优化建议
- 用
Get-CimInstance Win32_OperatingSystem代替Get-WmiObject,WMI已被微软弃用,CIM是更现代的替代方案。 - 脚本需以管理员权限运行,否则
Checkpoint-Computer和Windows Update操作会失败。 - 原脚本中
Start-Sleep -Second 10存在拼写错误,已修正为Start-Sleep -Seconds 10。
内容的提问来源于stack exchange,提问作者Smeddeu
相关产品推荐
相关产品推荐

