kbupdate icon indicating copy to clipboard operation
kbupdate copied to clipboard

New command to force install missing patches?

Open potatoqualitee opened this issue 5 years ago • 2 comments

Would be paired with https://github.com/potatoqualitee/kbupdate/issues/59

# get available updates for query
$UpdateSession = New-Object -com Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateupdateSearcher()
$SearchResult =  $UpdateSearcher.Search("Type!='Driver' and IsHidden=0 and IsInstalled=0")
  
# create a collection
$updateCollection=New-Object -com Microsoft.Update.UpdateColl
For($i=0; $i -lt $searchResult.Updates.Count; $i++){
    $update=$searchResult.Updates.Item($i)
    $update.EulaAccepted
    if(-not $update.EulaAccepted){
        Write-Host "Accepting EULA for $update" -ForegroundColor green
        $Update.AcceptEula()}
        [void]$updateCollection.Add($Update)
}

#display all updates selected
$updateCollection | 
    ForEach-Object {}
        $updateCollection|%{Write-Host ("{0,-10}{1,-30}" -f $($_.KBArticleIDs),$_.Title) -fore green
    }
 
# call downloader with collection
$downloader=$UpdateSession.CreateUpdateDownloader()
$downloader.Updates=$updateCollection
$Downloader.Download()
 
# call installer with same collection.
$installer = $UpdateSession.CreateUpdateInstaller()
$installer.ForceQuiet=$true
$installer.Updates=$updateCollection
$result=$Installer.Install()
if($result.ResultCode -eq 2){
    Write-host "  Installation Succeeded" -foregroundcolor green
    Write-Host "Reboot required $($result.RebootRequired)" -Fore  green
}else{
    Write-host "  INSTALLATION FAILED, check event log for details" -foregroundcolor red
}

https://social.technet.microsoft.com/Forums/en-US/5ac70b68-d935-42b7-868e-acd082b2e4b8/powershell-script-improvement-needed-for-windows-updates?forum=ITCG

potatoqualitee avatar Feb 03 '20 13:02 potatoqualitee

ayyy nice, thanks 🥔

potatoqualitee avatar May 12 '22 14:05 potatoqualitee

how to make this work in offline envs without a WSUS server tho? maybe that cab?

potatoqualitee avatar May 12 '22 14:05 potatoqualitee

ref: #132

https://github.com/rgl/packer-plugin-windows-update/blob/master/update/windows-update.ps1

potatoqualitee avatar Aug 04 '22 06:08 potatoqualitee

done in recent versions

potatoqualitee avatar Aug 06 '22 12:08 potatoqualitee