I'm trying to automate some Microsoft updates on Windows 7. I'm trying to find some already installed updates, then uninstall them. The weird thing is that I can do it for one KB, but not for another KB.
I use the same steps for each KB: find it in the installed history, get its historical UpdateID, then use the searcher to locate the specific item again to use with the Uninstall method later. I am unable to use the Searcher for one of the KBs, even though it is already on my system.
$updater = New-Object -ComObject Microsoft.Update.Session $searcher = $updater.CreateUpdateSearcher() $history = $searcher.QueryHistory(1,$searcher.GetTotalHistoryCount()) # retrieve the full history of installed updates $kbs = ('KB3068708','KB3022345') foreach ($kb in $kbs) { $removable = $history | where {$_.title -match $kb} if ($removable -ne $null) { $id = $removable.UpdateIdentity.UpdateID write-host "$kb found in history with UpdateID=$id" $search = 'UpdateID=''' + $removable.UpdateIdentity.UpdateID + '''' $removeit = $searcher.Search($search) if ($removeit.Updates.Count -gt 0) { write-host "Success! Able to search the update based on its historical updateIdentity.UpdateID value" } else { write-host "Failure! UNable to search the update based on its historical updateIdentity.UpdateID value" } } }
I get these results:
KB3068708 found in history with UpdateID=0cd9efd9-d371-4e7d-8381-15ae5b55ea79 Success! Able to search the update based on its historical updateIdentity.UpdateID value KB3022345 found in history with UpdateID=94ec92df-8687-4f9e-acca-2c4ac8bf3b19 Failure! UNable to search the update based on its historical updateIdentity.UpdateID valueWhat am I doing wrong here?