Solved

VBS Windows Update script

Posted on 2011-02-17
5
1,155 Views
Last Modified: 2012-06-27
I am writing a VBS script for use with MDT. This script is supposed to pull down and install all available windows updates for Windows XP clients. Occasionally I receive error 0x80240016, which if I understand correctly indicates that a reboot is necessary before a given update will install. My question is how can I handle this gracefully, without a reboot for every single update the requests one? As it stands my script errors out by throwing 0x80240016.
Based on my logging it looks like the code is stopping on the line 'Set installationResult = installer.Install()" line.
Function DoUpdate()

	Set updateSession = CreateObject("Microsoft.Update.Session")
	Set updateSearcher = updateSession.CreateupdateSearcher()
	Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
	Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
	Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

	For I = 0 to searchResult.Updates.Count-1
		Set update = searchResult.Updates.Item(I)
		updatesToDownload.Add(update)
		Set downloader = updateSession.CreateUpdateDownloader() 
		downloader.Updates = updatesToDownload
		downloader.Download()
		updatesToDownload.Clear()

		If update.IsDownloaded = true Then
			updatesToInstall.Add(update)	
			Set installer = updateSession.CreateUpdateInstaller()
			installer.Updates = updatesToInstall
			Set installationResult = nothing 
			Set installationResult = installer.Install()
			updatesToInstall.Clear()
			
			If ( installationResult is nothing OR installationResult.ResultCode <> 2 OR installationResult.RebootRequired ) Then
				oEnvironment.Item("SMSTSRebootRequested") = "true"
				oEnvironment.Item("SMSTSRetryRequested") = "true"
				If ContinueReboot( update.Title ) Then
					' Reboots aren't generally allowed from standard Windows Updates
					oLogging.CreateEntry "Immediate Reboot Allowed for " & update.Title, LogTypeInfo
					Exit For
				Else
					oLogging.CreateEntry "Immediate Reboot denied for " & update.Title, LogTypeInfo
				End If
			End If
		End If
	Next

End Function

Open in new window

0
Comment
Question by:timbrigham
  • 2
  • 2
5 Comments
 
LVL 47

Assisted Solution

by:Donald Stewart
Donald Stewart earned 100 total points
ID: 34920997
It appears that error means something other than a pending reboot



Error message when you try to install updates from the Windows Update Web site or from the Microsoft Update Web site: "0x80240016"

http://support.microsoft.com/kb/957307
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 400 total points
ID: 34922882
Hi, if the article Dave pointed to is correct, then perhaps you can detect the error, and restart wuauserv, and retry the install....

Regards,

Rob.
Function DoUpdate()

	Set updateSession = CreateObject("Microsoft.Update.Session")
	Set updateSearcher = updateSession.CreateupdateSearcher()
	Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
	Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
	Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

	For I = 0 to searchResult.Updates.Count-1
		Set update = searchResult.Updates.Item(I)
		updatesToDownload.Add(update)
		Set downloader = updateSession.CreateUpdateDownloader() 
		downloader.Updates = updatesToDownload
		downloader.Download()
		updatesToDownload.Clear()

		If update.IsDownloaded = true Then
			updatesToInstall.Add(update)	
			Set installer = updateSession.CreateUpdateInstaller()
			installer.Updates = updatesToInstall
			Set installationResult = nothing 
			On Error Resume Next
			Set installationResult = installer.Install()
			If Err.Number = 80240016 Then
				oLogging.CreateEntry "Restarting wuauserv service and retrying install of " & update.Title, LogTypeInfo
				Set objShell = CreateObject("WScript.Shell")
				objShell.Run "net stop wuauserv", 0, True
				WScript.Sleep 3000
				objShell.Run "net start wuauserv", 0, True
				WScript.Sleep 3000
				Err.Clear
				On Error Resume Next
				Set installationResult = installer.Install()
			End If
			updatesToInstall.Clear()
			
			If ( installationResult is nothing OR installationResult.ResultCode <> 2 OR installationResult.RebootRequired ) Then
				oEnvironment.Item("SMSTSRebootRequested") = "true"
				oEnvironment.Item("SMSTSRetryRequested") = "true"
				If ContinueReboot( update.Title ) Then
					' Reboots aren't generally allowed from standard Windows Updates
					oLogging.CreateEntry "Immediate Reboot Allowed for " & update.Title, LogTypeInfo
					Exit For
				Else
					oLogging.CreateEntry "Immediate Reboot denied for " & update.Title, LogTypeInfo
				End If
			End If
		End If
	Next

End Function

Open in new window

0
 
LVL 47

Expert Comment

by:Donald Stewart
ID: 34922902
Who's Dave ?  :^)
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34922946
Sorry, my bad....somewhere along the line I thought I saw that was your name....I apologise if I'm wrong...
0
 
LVL 1

Author Closing Comment

by:timbrigham
ID: 34945422
Thanks gentlemen. The examples given for the error handling were just the ticket.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now