header image

Windows Updates: 3 Installing Updates

Posted by: | June 11, 2011 | No Comment |

Now we have discovered the updates we have available we can think about installing them.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
function install-update {

$session = New-Object -ComObject Microsoft.Update.Session
$searcher = $session.CreateUpdateSearcher()

$result = $searcher.Search("IsInstalled=0 and Type=’Software’ and ISHidden=0" )

if ($result.Updates.Count -eq 0) {
 
Write-Host "No updates to install"
}
else {
$result.Updates | select Title
}

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

foreach ($update in $result.Updates){
 
$downloads.Add($update
)
}
 

$downloader = $session.CreateUpdateDownLoader()
$downloader.Updates = 
$downloads
$downloader
.Download()

$installs = New-Object -ComObject Microsoft.Update.UpdateColl
foreach ($update in $result.
Updates){
 
if ($update.
IsDownloaded){
  
$installs.Add($update
)
 }
}

$installer = $session.CreateUpdateInstaller()
$installer.Updates = 
$installs
$installresult
 = $installer.Install()
$installresult

}

 

The function looks for non-hidden software updates that aren’t hidden.  At the moment I want some control over driver updates. If there are updates available they are added to a download collection and then downloaded.

The update collection returned from our original search is automatically updated with the fact the update is now downloaded.

We create an installer, add the updates to it and install.

The function is still a bit rough but it works and better still illustrates how the COM objects work.

Next jobs are to tidy up the functions and extend the functionality to remote machines!

under: COM, IT Security, PowerShellV2