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: 11/18/2009



Community Content

Mason Huang
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

Tags :

ITWyatt
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
Tags : contentbug

N-able
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

Tags :

hs2n
WuInstall

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

Tags :

Ahto Metting
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]

Tags :

RDRuffner
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

Tags :

cs-it-solutions.de
Service Packs
How to install Service Packs using these methods in an unattended manner?
Tags :

Sonny the Admin
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

Tags :

Page view tracker