WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Windows Update Agent


;How to use the Windows Update Agent (WUA) to scan, download, and install a specific update. 
;
;The update can be specified by its title. The sample searches for a specific software update, downloads the update, 
;and then installs the update. For example, a user can use this method to determine if a critical security update 
;is installed on a specific computer. If the update is not installed, the user can ensure that the update is downloaded, 
;installed, and the user notified about the status of the installation. 
;
;The sample update is identified by the update title in Title Property of IUpdateHistoryEntry. The title of the update used 
;in this sample is “Update for Windows Rights Management client 1.0”. If you want to search, download, and install all the 
;updates that apply to a specific application, see Searching, Downloading and Installing Updates.
;
;Before you attempt to run this sample, note the following:
;
;You must have Windows Update Agent installed on the computer. For information about determining the version of WUA 
;installed, see Determining the Current Version of WUA. 
;
;The sample does not provide a user interface of its own. WUA prompts the user to reboot the computer if an update requires a reboot. 
;
;The sample can only download updates from WUA. It cannot download updates from a SUS 1.0 server.
;
;Example

title = "Windows Update Agent"
objUpdateSession = CreateObject("Microsoft.Update.Session")

objUpdateSearcher = objUpdateSession.CreateupdateSearcher()

;Search for all software updates, already installed and not installed
objSearchResult = objUpdateSearcher.Search("Type='Software'")

updateIsApplicable = @False
list = ""
;Cycle through search results to look for the update title
For i = 0 To objSearchResult.Updates.Count-1
      update = objSearchResult.Updates.Item(i)
    	list = StrCat(list,@tab,update.Title) 
		;update.IsInstalled 
Next
list = StrTrim(list)
updateTitle = AskItemlist("software Update List", list , @tab, @unsorted, @single)

Message(title, StrCat("Searching for: ", updateTitle , "..." ))

;Cycle through search results to look for the update title
For i = 0 To objSearchResult.Updates.Count-1
      update = objSearchResult.Updates.Item(i)
    	If StrUpper(update.Title) == StrUpper(updateTitle) 
			;Update in list of applicable updates 
			;Determine if it is already installed or not
			If update.IsInstalled == @False 
				Message(title,"Result: Update applicable, not installed.")
				updateIsApplicable = @True
				objUpdateToInstall.Add(update)
			Else 
				;Update is installed so notify user and quit
				Message(title,"Result: Update applicable, already installed.")
				updateIsApplicable = @True
				exit	
			EndIf
		EndIf
      update = 0
Next

If updateIsApplicable == @False 
	Message(title,"Result: Update is not applicable to this machine." )
	exit
EndIf

stdInput = AskYesNo(title, "Would you like to install now? (Y/N)")
	
If  stdInput == @YES
		;Download update
		downloader = objUpdateSession.CreateUpdateDownloader() 
		downloader.Updates = objUpdateToInstall
		downloadResult = downloader.Download()
		Message(title, StrCat("Download result ",downloadResult.ResultCode))
		
		;Install Update
		installer = objUpdateSession.CreateUpdateInstaller()
		installer.Updates = objUpdateToInstall
		installationResult = installer.Install()
		;Output the result of the installation
      Message(title, StrCat("Installation result ",		installationResult.ResultCode))

		Message(title, StrCat("Reboot Required: ",installationResult.RebootRequired ))
						
EndIf
objUpdateToInstall = 0
objSearchResult = 0
objUpdateSearcher =  0
objUpdateSession = 0

Article ID:   W17201
File Created: 2007:07:03:14:28:42
Last Updated: 2007:07:03:14:28:42