Windows 10或11使用PowerShell进行22H2功能更新。

huangapple go评论106阅读模式
英文:

Windows 10 or 11 Feature update to 22H2 using powershell

问题

我找到了“Feature update to Windows 10, version 22H2”更新对象并返回,但我仍然遇到了错误。当我查看变量$featureUpdate时,我可以看到正确的对象。

英文:

I would like to update all my windows 10 and windows 11 machines with a Powershell script.
I have a RMM agent installed on those computers and i want to invoke the OS to install the 22H2 Feature update. (My RMM Agent doesn't have a proper Patch/Windows update management, thats why i'm will to try it via a Powershell script)
For some reason i'm hitting against the wall and receive the following error:

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

My code:

# 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."
}

The proper "Feature update to Windows 10, version 22H2" update object is found and returned, but i still get a error.
I don't know what i'm doing wrong here.

Thanks in advance for your help!

The proper "Feature update to Windows 10, version 22H2" update object is found and returned, but i still get a error.
When review the variable $featureUpdate i can see the correct object.

答案1

得分: 1

你需要将更新作为 Microsoft.Update.UpdateColl 对象提供。

$updatelist = New-Object -ComObject Microsoft.Update.UpdateColl

# 在此示例中只有一个,但我们会为更多的情况编写代码
foreach($update in $featureupdate){
    $null = $updatelist.Add($update)
}

现在您可以将您的更新添加到下载器列表中

$downloader.updates = $updatelist
英文:

You need to provide the update(s) as a Microsoft.Update.UpdateColl object.

$updatelist = New-Object -ComObject Microsoft.Update.UpdateColl

# There is only one in this instance, but we'll go ahead and code for more
foreach($update in $featureupdate){
    $null = $updatelist.Add($update)
}

Now you can add your updates to the downloader list

$downloader.updates = $updatelist

huangapple
  • 本文由 发表于 2023年5月7日 00:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定