Hey, Scripting Guy! Question

Hey, Scripting Guy! I am curious about the updates that are available from Windows Update. The whole process seems so nebulous. It is like some kind of Impressionist painting that suggests things, but I never really get a feel for the updates that are coming down or the ones that are available. I like having a better handle on things than this. Do you have a script that will tell me which software updates are available via Windows Update? If I could see the list, then I would understand better what is really going on. Does this make sense?

- TC

SpacerHey, Scripting Guy! Answer

Hi TC,

Sure it makes sense. Incidentally, I love the Impressionists, who were as it turns out named after a Claude Monet painting "Impression, soleil levant de retour." Like the interplay between the light and the shadows that enthralled the Impressionists, these are the kinds of questions that I hate to get sometimes. Why? Well, I do not want to be like a lot of people who might say, "Why would you want to do that?" I figure, hey, you are an adult (perhaps) and if you want to do something, go ahead and do it if you wish. Anyway, we can certainly create a script that will return all the software updates from the Windows Update Web site. What you decide to do with such a list is up to you. We will call the script, ListSoftwareUpdates.ps1, and I can write that script in three lines. (Actually, I can write the script in a single line. I just prefer the three-line version for readability.)

This week we will be looking at using the Windows Update API to work with Windows Update. The Windows Update API is documented on MSDN. There is also a good collection of VBScripts that use the Windows Update API in the Script Center Script Repository. For information about manually configuring Windows Update on workstations, refer to this page. These scripts use Windows PowerShell. The Windows PowerShell getting started page has basic information for learning about Windows PowerShell and about downloading Windows PowerShell.

For a VBScript version of the ListSoftwareUpdates.ps1 script, check the Script Center Script Repository. The complete ListSoftwareUpdates.ps1 script is seen here.

$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Results = $Searcher.Search("Type='Software'")
$Results.Updates

The script begins by creating a COM object. To do this, we use the New-Object cmdlet with the –ComObject parameter. We do not need to surround the program ID of the COM object in quotation marks because the –ComObject parameter already expects a string. We store the returned Searcher object (IUpdateSearcher interface) in the $Searcher variable. The line of the ListSoftwareUpdates.ps1 script that creates the Searcher object is seen here:

$Searcher = New-Object -ComObject Microsoft.Update.Searcher

The Searcher object can do more than just search for updates on the server; it can also query the history, get the history count, and do other things. The members of the Searcher object are seen in Table 1.

Table 1 Members of the Searcher object
Name Type Definition

BeginSearch

Method

ISearchJob BeginSearch (strin

EndSearch

Method

ISearchResult EndSearch (ISea

EscapeString

Method

string EscapeString (string)

GetTotalHistoryCount

Method

int GetTotalHistoryCount ()

QueryHistory

Method

IUpdateHistoryEntryCollection

Search

Method

ISearchResult Search (string)

CanAutomaticallyUpgradeService

Property

bool CanAutomaticallyUpgradeS

ClientApplicationID

Property

string ClientApplicationID ()

IgnoreDownloadPriority

Property

bool IgnoreDownloadPriority (

IncludePotentiallySupersededUpdates

Property

bool IncludePotentiallySupers

Online

Property

bool Online () {get} {set}

SearchScope

Property

SearchScope SearchScope () {g

ServerSelection

Property

ServerSelection ServerSelecti

ServiceID

Property

string ServiceID () {get} {set}

Because we are interested in searching for all the software updates, we use the Search method. The custom search language that is used with the Search method is documented on MSDN. Basically it consists of a property and an associated value for that property. In the ListSoftwareUpdates.ps1 script, we are looking for a search result type that is equal to Software. This line of the script is seen here:

$Results = $Searcher.Search("Type='Software'")

The custom search language that is used with the Search method allows us to use the and operator and the or operator. If we wanted to find software updates that were not installed, we could use this line of code:

$results = $searcher.search("Type='software' AND IsInstalled = 0")

Tthe MSDN documentation for the Search method tells us that the IsInstalled property is an integer (Boolean) value. The search criteria produces an error when I try to use $true or $false. This means we are back to the old-fashioned days of using 0 for false and 1 for true (it will not even accept -1).

The $results variable contains a Results object (the ISearchResult Interface is documented here). This object consists of four properties, which are seen in Table 2.

Table 2 SearchResult object members
Name Type Definition

ResultCode

Property

OperationResultCode ResultCode () {get}

RootCategories

Property

ICategoryCollection RootCategories () {get}

Updates

Property

IUpdateCollection Updates () {get}

Warnings

Property

IUpdateExceptionCollection Warnings () {get}

In today’s script we are interested in the collection of software updates that is obtained by querying the Updates property. When we do this, all of the software updates are listed as seen here:

Image of the listing of all software updates

 

The line of code that displays all the software updates is seen here.

$Results.Updates

Here is a bonus script. The DisplayTheSoftwareUpdateSettingsDialogBox.ps1 script uses the Microsoft.Update.AutoUpdate COM object to show the settings dialog box. To do this, we use the New-Object cmdlet and specify the –ComObject parameter and supply the Microsoft.Update.AutoUpdate program ID. We put the New-Object command inside a pair of parentheses and call the ShowSettingsDialog method as seen here. The complete DisplayTheSoftwareUpdateSettingsDialogBox.ps1 script is seen here (the VBScript version of this script can be seen in the Script Center Script Repository):

(New-Object -ComObject Microsoft.Update.AutoUpdate).ShowSettingsDialog()

When the script is run, it produces the dialog box seen here:

Image of the dialog box that appears when the bonus script is run

 

Well, TC, that is about all there is to searching for specific types of software updates. And you even got a bonus script out of it. I hope this removes some of the mystery from the Windows Update process. Join us tomorrow when we will continue looking at managing Windows Update. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys