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

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

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:

  1. Index was outside the bounds of the array.
  2. At line:42 char:5
  3. + $downloader.Updates = $featureUpdate
  4. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. + CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException
  6. + FullyQualifiedErrorId : System.IndexOutOfRangeException
  7. Exception from HRESULT: 0x80240004

My code:

  1. # Check if running on Windows 10 or Windows 11
  2. $osVersion = (Get-WmiObject -Class Win32_OperatingSystem).Version
  3. if (-not ($osVersion -match "^10\." -or $osVersion -match "^11\.")) {
  4. Write-Host "No Windows 10 or Windows 11 detected! Exiting..."
  5. exit
  6. }
  7. # Check if Windows Update service is running
  8. $wuaService = Get-Service -Name wuauserv
  9. if ($wuaService.Status -ne "Running") {
  10. Write-Host "Windows Update service is not running. Please start the service and try again."
  11. exit
  12. }
  13. # Check if System Restore is enabled
  14. if ((Get-ComputerRestorePoint).Count -eq 0) {
  15. Write-Host "System Restore is not enabled on this system. Please enable it and try again."
  16. exit
  17. }
  18. # Create a system restore point
  19. $timestamp = Get-Date -Format "dd-MM-yyyy HH:mm:ss"
  20. $restorePointDescription = "Restorepoint - $timestamp"
  21. Checkpoint-Computer -Description $restorePointDescription -RestorePointType "MODIFY_SETTINGS"
  22. # Create a Windows Update session
  23. $wuaSession = New-Object -ComObject Microsoft.Update.Session
  24. # Search for available updates
  25. $searcher = $wuaSession.CreateUpdateSearcher()
  26. $updates = $searcher.Search("IsInstalled=0 and DeploymentAction=*")
  27. # Filter updates to find the feature update
  28. $featureUpdate = $updates.Updates | Where-Object {
  29. $_.Title -like ("*Feature Update*")
  30. }
  31. # Check if a feature update is available
  32. if ($featureUpdate) {
  33. # Download and install the feature update
  34. $downloader = $wuaSession.CreateUpdateDownloader()
  35. $downloader.Updates = $featureUpdate
  36. $downloader.Download()
  37. $installer = $wuaSession.CreateUpdateInstaller()
  38. $installer.Updates = $featureUpdate
  39. $installResult = $installer.Install()
  40. # Check the installation result
  41. if ($installResult.ResultCode -eq "2") {
  42. Write-Host "Feature update installed successfully. Please restart"
  43. Start-Sleep -Second 10
  44. exit
  45. } else {
  46. Write-Host "Feature update installation failed with error code $($installResult.ResultCode)."
  47. }
  48. } else {
  49. Write-Host "No feature update is available."
  50. }

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 对象提供。

  1. $updatelist = New-Object -ComObject Microsoft.Update.UpdateColl
  2. # 在此示例中只有一个,但我们会为更多的情况编写代码
  3. foreach($update in $featureupdate){
  4. $null = $updatelist.Add($update)
  5. }
  6. 现在您可以将您的更新添加到下载器列表中
  7. $downloader.updates = $updatelist
英文:

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

  1. $updatelist = New-Object -ComObject Microsoft.Update.UpdateColl
  2. # There is only one in this instance, but we'll go ahead and code for more
  3. foreach($update in $featureupdate){
  4. $null = $updatelist.Add($update)
  5. }

Now you can add your updates to the downloader list

  1. $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:

确定