简体   繁体   中英

Windows Update Agent API - Searching for Updates

I wrote a Powershell script that uses the Windows Update Agent API (IUpdateSearcher, IUpdateDownloader, IUpdateInstaller etc.). Everything works fine, the script finds avaiable updates, downloads and installs them.

However, there is a problem when searching for consecutive updates. For example, there is an update for the .Net Framework 4.5.2. The update is installed by script and the PC is rebooted afterwards. Now there should be an update for the .Net Framework 4.5.2 Language Pack avaiable.

But it is not. At least not via the API. A manual search with the GUI (Windows Update) works.

After the manual search, the API finds the update a well!

What am I missing in my script? I could not find anything in Microsofts documentation: https://msdn.microsoft.com/en-us/library/windows/desktop/aa386868(v=vs.85).aspx

$updateSession = New-Object -ComObject 'Microsoft.Update.Session'
$UpdateSession.WebProxy.AutoDetect = $false

$updateSearcher = $updateSession.CreateUpdateSearcher()

$searchResult = $updateSearcher.Search('IsInstalled=0 and IsHidden=0')

$objCollectionDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'

foreach ($update in $searchResult.Updates)
{
    $objCollectionTmp = New-Object -ComObject 'Microsoft.Update.UpdateColl'
    $objCollectionTmp.Add($update) | Out-Null

    $downloader = $updateSession.CreateUpdateDownloader()
    $downloader.Updates = $objCollectionTmp

    try
    {
        $downloadResult = $downloader.Download()
    }
    catch
    {
        //exception Handling
    }

    $objCollectionDownload.Add($update) | Out-Null
}

$updatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'
$updateInstaller = $updateSession.CreateUpdateInstaller()

foreach ($update in $objCollectionDownload)
{
    //accept Eula etc...

    $updatesToInstall.Add($update) | Out-Null
}

$updateInstaller.Updates = $updatesToInstall

$installationRestult = $updateInstaller.Install()

//check installation result

Oddly enough I had the same issue just now, Windows GUI showed a particular update, Our GUI using the API wouldn't show this particular update... I had IsInstalled = 0 and IsHidden = 0.... I looked in the WIndows Update log and found the criteria that the WIndows GUI was using.

IsInstalled=0 and DeploymentAction='Installation' or IsPresent=1 and DeploymentAction='Uninstallation' or IsInstalled=1 and DeploymentAction='Installation' and RebootRequired=1 or IsInstalled=0 and DeploymentAction='Uninstallation' and RebootRequired=1

Added this to my application in place of IsInstalled = 0 and IsHidden = 0 and the update showed straight up :-/ don't really understand why but I am not complaining.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM