我想用 Powershell 脚本更新我所有的 Windows 10 和 Windows 11 机器。 我在这些计算机上安装了 RMM 代理,我想调用操作系统来安装 22H2 功能更新。 (我的 RMM 代理没有适当的补丁/Windows 更新管理,这就是为什么我要通过 Powershell 脚本尝试它) 出于某种原因,我撞到墙上并收到以下错误:
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
我的代码:
# 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."
}
找到并返回了正确的“Windows 10 功能更新,版本 22H2”更新对象,但我仍然收到错误。 我不知道我在这里做错了什么。
提前感谢您的帮助!
找到并返回了正确的“Windows 10 功能更新,版本 22H2”更新对象,但我仍然收到错误。 查看变量 $featureUpdate 时,我可以看到正确的对象。