windowsvbscriptwuapi

How does CreateUpdateDownloader know to download what files?


How does CreateUpdateDownloader download files? I ask because my system is missing 4 KBs.

I get the title of the 4 missing KBs from iterating through an update collection in my script.

When I assign that collection to a CreateUpdateDownloader though, I only find 1 KB in C:\Windows\SoftwareDistribution\Download.

Any thoughts why it didn't download the other 3 KBs? Yes, I'm only looking to scan and download for right now -- trying to learn how this works by watching it in action. I'll get to install later as I want to tweak some of that.

The code follows:

Dim session : Set session = CreateObject("Microsoft.Update.Session")
Dim search  : Set search  = session.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCrLF

Set result = search.Search("IsInstalled=0 AND Type='Software' AND IsHidden=0")

WScript.Echo "Missing KBs:"
For i = 0 To result.Updates.Count -1 'last item in the collection always seems to be some kind of gibberish null.
    Set update = result.Updates.Item(i)
    WScript.Echo i + 1 & "> " & update.Title
Next

If result.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
End If

Set downloader = session.CreateUpdateDownloader() 
downloader.Updates = result.Updates ' updatesToDownload
downloader.Download()

Solution

  • It must be used Microsoft.Update.UpdateColl to collect updates to download. Function CopyFromCache allows to download a local copy of update. Propertie DownloadURL will allow you to get download from Internet. It was very useful iupdate object documentation

    This is my "first" approach to code. First 5 updates are downloaded to d:\updates directory and their corresponding URL are listed.

    Dim session : Set session = CreateObject("Microsoft.Update.Session")
    Dim search  : Set search  = session.CreateUpdateSearcher()
    WScript.Echo "Searching for updates..." & vbCrLF
    Set result = search.Search("IsInstalled=0 AND Type='Software' AND IsHidden=0")
    WScript.Echo "Missing KBs:"
    For i = 0 To result.Updates.Count -1 'last item in the collection always seems to be some kind of gibberish null.
        Set update = result.Updates.Item(i)
        WScript.Echo i + 1 & "> " & update.Title
    Next
    If result.Updates.Count = 0 Then
        WScript.Echo "There are no applicable updates."
    End If
    
    Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
    Set downloader = session.CreateUpdateDownloader() 
    'For I = 0 to result.Updates.Count-1
    For I = 0 to 5
        Set update = result.Updates.Item(I)
        updatesToDownload.Add(update)
    Next
    WScript.Echo vbCRLF & "Downloading updates..."
    downloader.Updates = updatesToDownload
    downloader.Download()
    
    'For I = 0 to result.Updates.Count-1
    for i=0 to 5
      for each upd in downloader.Updates.Item(i).BundledUpdates
       upd.CopyFromCache "d:\UPDATES", False
       for each content in upd.DownloadContents
         wscript.echo "url: " & content.DownloadURL
       next 
      next 
    next