通过Powershell IndexOutOfRangeException进行Windows更新
我正在尝试使用来自的COM对象通过PowerShell安装windows更新 这是我到目前为止得到的代码通过Powershell IndexOutOfRangeException进行Windows更新,windows,powershell,windows-update,wuapi,Windows,Powershell,Windows Update,Wuapi,我正在尝试使用来自的COM对象通过PowerShell安装windows更新 这是我到目前为止得到的代码 $updateSession = New-Object -com Microsoft.update.Session $updateSearcher = $UpdateSession.CreateUpdateSearcher() $updateResult = $updateSearcher.Search("IsInstalled=0 and Type='Software'"
$updateSession = New-Object -com Microsoft.update.Session
$updateSearcher = $UpdateSession.CreateUpdateSearcher()
$updateResult = $updateSearcher.Search("IsInstalled=0 and Type='Software'");
$needsRestart = $false
foreach($update in $updateResult.Updates) {
$needsRestart = $needsRestart -or $update.InstallationBehavior.RebootBehavior -ne 0
}
$updateDownloader = $UpdateSession.CreateUpdateDownloader()
$updateDownloader.Updates = $updateResult.Updates
$downloadResult = $updateDownloader.Download()
当我运行这段代码时,我会得到IndexOutOfRangeException
Index was outside the bounds of the array.
At C:\Users\MyUser\Documents\Update-Windows2.ps1:9 char:1
+ $updateDownloader.Updates = $updateResult.Updates
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], IndexOutOfRangeException
+ FullyQualifiedErrorId : System.IndexOutOfRangeException
我检查了又检查,似乎找不到问题所在。我用C#代码尝试过类似的逻辑,它似乎能够很好地分配Updates
变量,没有任何问题
你知道我在这里遗漏了什么吗?提前感谢。无法复制,但我非常确定“$updateResult.Updates”为$null(=无可用更新) 你能检查一下吗 如果是,请添加一个If条件($与集合一起使用时左侧为null!)
if($null-ne$updateResult.Updates){
$updateDownloader.Updates=$updateResult.Updates
$downloadResult=$updateDownloader.Download()
}
为什么$null在左边?(不考虑PS版本):
无法复制。这对我来说很好。我今天确实再试了一次,但再也没有了。我想可能是机器重启解决了这个问题。我想它可能是由于COM对象内存泄漏而触发的。谢谢你的回复。谢谢你的回复。我想重启机器已经解决了这个问题。可能是某个COM内存泄漏触发了此事件?