23 out of 32 rated this helpful - Rate this topic

Searching, Downloading, and Installing Updates

The scripting sample in this topic shows you how to use Windows Update Agent (WUA) to scan, download, and install updates.

The sample searches for all the applicable software updates and then lists those updates. Next, it creates a collection of updates to download and then downloads them. Finally, it creates a collection of updates to install and then installs them.

If you want to search, download, and install a specific update that you identify by using the update title, see Searching, Downloading, and Installing Specific Updates.

Before you attempt to run this sample, note the following:

  • WUA must be installed on the computer. For more information about how to determine the version of WUA that is installed, see Determining the Current Version of WUA.
  • The sample does not provide its own user interface.
  • WUA prompts the user to restart the computer if an update requires a restart.
  • The sample can download updates only by using WUA. It cannot download updates from a Software Update Services (SUS) 1.0 server.
  • Running this sample requires Windows Script Host (WSH). For more information about WSH, see the WSH section of the Platform Software Development Kit (SDK). If the sample is copied to a file named WUA_SearchDownloadInstall.vbs, you can run the sample by opening a Command Prompt window and typing the following command at the command prompt.

    cscript WUA_SearchDownloadInstall.vbs

Example



Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

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

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software'")


WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title
Next

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

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> adding: " & update.Title 
    updatesToDownload.Add(update)
Next

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

WScript.Echo  vbCRLF & "List of downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    If update.IsDownloaded Then
       WScript.Echo I + 1 & "> " & update.Title 
    End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

WScript.Echo  vbCRLF & _
"Creating collection of downloaded updates to install:" 

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
       WScript.Echo I + 1 & "> adding:  " & update.Title 
       updatesToInstall.Add(update)	
    End If
Next

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "N" or strInput = "n") Then 
	WScript.Quit
ElseIf (strInput = "Y" or strInput = "y") Then
	WScript.Echo "Installing updates..."
	Set installer = updateSession.CreateUpdateInstaller()
	installer.Updates = updatesToInstall
	Set installationResult = installer.Install()
	
	'Output results of install
	WScript.Echo "Installation Result: " & _
	installationResult.ResultCode 
	WScript.Echo "Reboot Required: " & _ 
	installationResult.RebootRequired & vbCRLF 
	WScript.Echo "Listing of updates installed " & _
	 "and individual installation results:" 
	
	For I = 0 to updatesToInstall.Count - 1
		WScript.Echo I + 1 & "> " & _
		updatesToInstall.Item(i).Title & _
		": " & installationResult.GetUpdateResult(i).ResultCode 		
	Next
End If
		


 

 

Send comments about this topic to Microsoft

Build date: 7/7/2011

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Not working both on my win7 and win2k8r2 machines

I tried the script on both the 2 machines and it always return "There are no applicable updates.".

And i've tried to use c# code like:

 IUpdateSession updateSession = new UpdateSession();
 IUpdateSearcher updateSearcher = updateSession.CreateUpdateSearcher();
 updateSearcher.Online = true;
 ISearchResult uResult = updateSearcher.Search("IsInstalled=0 and Type='Software'");
 foreach (IUpdate update in uResult.Updates)
 {
 Console.WriteLine(update.Title);
 }

to get the updates. Only 1 of 100 times i got the expected updates which are the same with what i saw in "All Control Panel Items\WIndows Update".

So how could i accurately get the new updates?

Just one more example
The example code below may allow to understand how to find some "particular" updates; for example only the updates belonging to a given category or so on; by the way the code doesn't much more than just listing all the installed and available updates but it may help a bit in case one needs to write code to only fetch some particular updates and/or to bypass a local WSUS server <pre> Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, fp Dim objSearcher, objResults, objCategories Dim colUpdates Dim i, z, sQuery, sCat sQuery = "Type='Software'" ' "IsInstalled=0 and Type='Software'" Set fso = CreateObject("Scripting.FileSystemObject") WScript.StdOut.WriteLine "Initializing update object..." Set objSearcher = CreateObject("Microsoft.Update.Searcher") objSearcher.ServerSelection = 2 ' ssWindowsUpdate objSearcher.Online = True ' bypass WSUS server WScript.StdOut.WriteLine "Searching for updates, wait please..." Set objResults = objSearcher.Search(sQuery) Set colUpdates = objResults.Updates WScript.StdOut.WriteLine "Writing updates list to file..." Set fp = fso.OpenTextFile("updt.log", ForWriting, True) For i = 0 to colUpdates.Count - 1 Set objCategories = colUpdates.Item(i).Categories sCat = "" For z = 0 to objCategories.Count - 1 sCat = sCat & "," & objCategories.Item(z).Name Next sCat = Mid(sCat, 2) fp.WriteLine "Update Title......: " & colUpdates.Item(i).Title fp.WriteLine "Categories........: " & sCat fp.WriteLine "Description.......: " & colUpdates.Item(i).Description fp.WriteLine "Max download size.: " & colUpdates.Item(i).MaxDownloadSize fp.WriteLine "Downloaded........: " & colUpdates.Item(i).IsDownloaded fp.WriteLine "Installed.........: " & colUpdates.Item(i).IsInstalled fp.WriteLine "" Next fp.Close WScript.StdOut.WriteLine "All done, exiting." WScript.Quit 0 </pre>
Who wrote this!!
Hey, please tell the world who wrote this....
WSUS server
The above script, how does it interact with a WSUS server defined, either via GPO or direct registry settings. Will it check with the windows update site if nothing is defined and check with a WSUS server if the settings are present. Or will it always check in with the windows update site.
Critical or Important details
http://msdn.microsoft.com/en-us/library/aa386099(v=VS.85).aspx MsrcSeverity will get your the rating of the patch. You can check for a specific rating (i.e. CRITICAL) and use code to process these only. #=============================================== Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl") For I = 0 to searchResult.Updates.Count-1 Set update = searchResult.Updates.Item(I) 'DO YOUR WORK HERE BASED ON MsrcSeverity check - FOR EXAMPLE: If update.MsrcSeverity = "Important" OR update.MsrcSeverity = "Critical" Then wscript.echo ("This item is " & update.MsrcSeverity & " and will be processed!") wscript.echo(I + 1 & "> adding: (" & update.MsrcSeverity & ") " & update.Title) updatesToDownload.Add(update) End If Next #=============================================== #EDIT: Sorry for formatting.
Running this script remotely
Hey how can I run the script above on a remote machine?
Installing updates silently
To answer some of the questions about "How can I install update X without it presenting its normal user interface?":

If you use the IUpdateInstaller2 interface instead of the IUpdateInstaller interface, then you can set the IUpdateInstaller2 object's ForceQuiet property to TRUE before calling Install or BeginInstall. The ForceQuiet property tells all the updates in the install batch, 'You cannot present a user interface -- either run silently or fail.' If an update supports silent install, then it will run without presenting a user interface. If the update does not support silent install, then it will fail with the error WU_E_UH_DOESNOTSUPPORTACTION.

If you want to silently install an update that returns WU_E_UH_DOESNOTSUPPORTACTION when ForceQuiet is TRUE, then you can't do it using the WUA API -- your only alternative at that point is to find a standalone version of the update's installer and see if that standalone version supports silent install.

install IE 8 silently
This script is awesome but is it possible to install IE8 silently using the default options?

It pops up and asks to Install IE8 right in the middle of the process, would be much better if it did it automatically.

Should I just have a batch script install IE 8 silently and then run the update script?
Client Not Updating with WSUS
Hello, 

  In our Network Configured a WSUS server on 2003 OS, Update server working fine all clients systems getting updates frm it, but one server(2008) not updating from WSUS server 

Help me,

Thanks

Tigen

I got my answer! :D
http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/4086d252-cf56-4618-85cf-c729d5be0011?prof=required
Narrowing Down Search Results
I can think of a few ways to narrow down search results that i've found.  To understand this though its important that you look at the API information available here, which gives more information on what you can do:

http://msdn.microsoft.com/en-us/library/aa386099(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/aa385841(v=VS.85).aspx

One method would be to add to "AutoSelectOnWebSites=1" to your search query.
e.g. updateSearcher.Search("IsInstalled=0 and Type='Software' and AutoSelectOnWebSites=1")
This appears to only select what would automatically be selected when navigating to the site, which is generally only "High Priority".

Another method would be to try and parse through category name and grab things like "Security Rollups" or "Update Rollups" this looks tricky though.
They parse through categories in this example on drivers:
http://gallery.technet.microsoft.com/ScriptCenter/en-us/9dae48a2-b373-4345-803a-21e69926ebb8

Another method would be to control what your clients see through WSUS.  This method requires a domain and group policy.  This way you don't have to worry about scripting, just let them see what you want deployed and deploy your script.  This also allows you to deploy software optional stuff you want like .NET, Silverlight, Service Packs, etc.  I would say this is the preferred method in an Enterprise environment.

Best of Luck.

"Important" updates only
lol.  Everyone else gets their questions answered.  :(  ;)  

any idea how to modify this script and have it run the same process installing "Important" updates ONLY.  
Excluding "Recommended" updates.  

pretty please. :D
Accept EULA
I found you can accept EULA's using code from this website:  http://theether.net/kb/100121

WScript.Echo vbCRLF & "Creating collection of updates to download:"
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> adding: " & update.Title 
    updatesToDownload.Add(update)
    If update.EulaAccepted = False Then 
        update.AcceptEula
        WScript.Echo I + 1 & ">     Accept EULA " & update.Title 
    End If

Next
Please help - allow ONLY "Important" updates to be installed. How?
Hello, 

How do we alter this script to ONLY download & install the "Important" updates (normal defaults when manually downloading defaults)?  

We don't want to deploy "Recommended updates" if not required.  This can be bad for production.  

Thanks!
Automator
Is there any way to accept the EULA for an update?

Otherwise is there a method to catch the error and continue?

0x80240023

I'm running into this with new systems. Some of them have to update without the WSUS.
PowerShell
Hello, 

How do I get this to work with PowerShell? 
PowerShell
How can I get this to work with PowerShell?  
How to Install ONLY critical updates
Hello, 

How do we alter this script to ONLY download & install the critical updates (normal defaults when manually downloading defaults)?  

We don't want to deploy "Recommended updates" if not required.  ;) 

Thanks!
No Worky

I've run "cscript //h:cscript" and now there are no dialog boxes which is fine, but the script still errors at line 6 char 1.

C:\Documents and Settings\svinyard>\\NetServer\Shared\WSUS.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Searching for updates...

\\NetServer\Shared\WSUS.vbs(6, 1) (null): 0x80248011

Service Packs
How to install Service Packs using these methods in an unattended manner?
Dialog boxes

To those getting dialog boxes when running this script, executing:

cscript //h:cscript

once before using this script will set the default for scripting engine to always use cscript

RE: Support for 2008 Core Server

Use a command called "cscript" and then a script.
example this script shown above.: cscript WUA_SearchDownloadInstall.vbs
I used the script on my Windows Server 2008 Standard core ed and it works :)

IT engineer
A.Metting
Estonia

[quote:]"Has anyone tried this script on Windows 2008 Core Server?

I'm getting results that differ from expected.

Tthe script outputs to a dialog box which only has an OK button so when prompted to 'install updates (Y/N)' the script fails.
If you have any suggestions I would appreciate it.

Thanks[/quote]

WuInstall

the command line tool WuInstall.exe (http://www.xeox.com/index.php/en/tools/wuinstall) basically does something similar with some further options

Support for 2008 Core Server
Has anyone tried this script on Windows 2008 Core Server?

I'm getting results that differ from expected.

Tthe script outputs to a dialog box which only has an OK button so when prompted to 'install updates (Y/N)' the script fails.
If you have any suggestions I would appreciate it.

Thanks

Result codes
The result codes for overall result and individual updates installed by the script can be found as an enumeration here: http://msdn.microsoft.com/en-us/library/aa387095(VS.85).aspx
Who can help me please?

Hi there,

When I try to run this script, it always has error on install updates. have any one please help me? Which part should I check?

OS: Xp pro sp3

WSUS 3.0 sp1

WUA version 7.2.6001.784

error message:

Would you like to install updates now? (Y/N) y
Installing updates...
Installation Result: 4
Reboot Required: False
Listing of updates installed and individual installation results:
1> Update for Microsoft Office Outlook 2003 Junk Email Filter (KB957257): 4