G
G
Gennady2017-07-20 14:23:58
PowerShell
Gennady, 2017-07-20 14:23:58

Problem installing updates, what's wrong?

Good day everyone!
There is this code:

$searcher = New-Object -ComObject Microsoft.Update.Searcher
$session = New-Object -ComObject Microsoft.Update.Session
$installer = New-Object -ComObject Microsoft.Update.Installer


$searcher.Online = $true
$searcher.ServerSelection=1
$results = $searcher.Search("IsInstalled=0")
$updates = $results.Updates

$updateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
ForEach($update in $updates)    {     $updateCollection.Add($update)      }

$downloader = $session.CreateUpdateDownloader()
$downloader.Updates = $updateCollection
$downloader.Download()

$installer.Updates = $updateCollection
$installer.Install()

When executed, there is a gag on the last two lines:
Index was outside the bounds of the array.
At line:24 char:1
+ $installer.Updates = $updateCollection
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException
 
Exception from HRESULT: 0x80240004
At line:25 char:1
+ $installer.Install()
+ ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Everything is OK in the $updates array, all updates are there.
Can anyone help?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question