I'm working on a PowerShell script to do some Windows Update tasks. Most tasks center around getting a collection of Windows Updates that have not yet been applied, using the code snippet below. Once that collection is returned, I iterate through it and perform such tasks as hiding, downloading, or installing the updates.
I notice that this code can take anywhere from 6 to 115 sceconds to run. Typically the longer runs are after the machine has restarted or been idle for more than 15 minutes.
But if I open the Windows Update control panel item, it instantly knows how many updates are outstanding, and can give me a list (collection) of those outstanding updates. If I click WU's "check for updates" link, it will take >10 seconds to check again, and sometimes that check will yield different results than it "knew" instantly when I opened it.
So I assume that WUA maintains a cached collection of updates somewhere, probably updated automatically once daily. My question: how can my code access that cache, rather than running the longer "check for updates" code shown below? Specifically, I am hoping to quickly obtain an IUpdateCollection to work with.
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$Searcher.Online = $false #tested $true and $false; $true slightly slower
$Criteria = "IsInstalled=0 and Type='Software'"
$SearchResult = $Searcher.Search($Criteria)
$SearchResult.Updates
Note that all this is happening on a current, Windows2012R2 system.