VBS script to start Windows Updates installation

Hector2016Systems Administrator and Solutions Architect
CERTIFIED EXPERT
Published:
Updated:
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Microsoft has implemented the Windows Update as a three-stage process. This is the key to understand how Windows Update works. The process needs to find out which new updates are available for each Microsoft product, download and install them all.

Windows-Update-Process.jpgThere are only two native ways to start the update process:
  • by using the control panel
Always-check.jpg
  • or by using the wuauclt tool.
WuAuClt /DetectNow

Open in new window

The use of the control panel implies the user interaction and the WuAuClt tool does not start the installation of the downloaded updates, it can only start the search. With the WuAuClt tool, the download will automatically follow the search only if the Windows Update settings on the client computer instructs to download any available update without asking the user.

So, there is a lack of native tools to start and complete the update process at will from a command prompt or from a scheduled task. To meet this need I created a script that executes the installation process from start to finish without user interaction. The script follows the three-stages process described previously, by using special windows functions published on MSDN for managing windows updates.

The script algorithm is the following:

  1. Verify if there is a pending reboot, if so: reboot right now unless the /nr option was entered.
  2. Search for updates.
  3. If there are new updates then download them all.
  4. Install all downloaded updates.
  5. Reboot if it is needed, unless the /nr option was entered..
 
On Error Resume Next
                      WScript.StdOut.Write "*****************************************************************" & vbCrLf
                      WScript.StdOut.Write "***         Forced install of all pending updates             ***" & vbCrLf
                      WScript.StdOut.Write "*****************************************************************" & vbCrLf 
                      WScript.StdOut.Write "**                    How to use this script                   **" & vbCrLf
                      WScript.StdOut.Write "*****************************************************************" & vbCrLf
                      WScript.StdOut.Write "** cscript DoUpdate.vbs [/nr]                                  **" & vbCrLf
                      WScript.StdOut.Write "**                                                             **" & vbCrLf
                      WScript.StdOut.Write "** [/nr]      Never reboot (default is to reboot if needed)    **" & vbCrLf
                      WScript.StdOut.Write "*****************************************************************" & vbCrLf
                      ' See if can auto-reboot.
                      DoReboot = True
                      if WScript.Arguments.Count <> 0 then
                        for i = 0 to WScript.Arguments.Count - 1
                          strInput1 = Lcase(Trim(WScript.Arguments(i)))
                      	
                      	if (strInput1 = "/nr") then
                            DoReboot = False	'Do not reboot even if it is necessary.
                          end if	
                      	
                        next
                      end if
                      '*******************************************************************
                      ' Create needed objects.
                      '*******************************************************************
                      Set updateSession = CreateObject("Microsoft.Update.Session")
                      Set updateSearcher	 = updateSession.CreateUpdateSearcher()
                      Set updateDownloader = updateSession.CreateUpdateDownloader()
                      Set updateInstaller  = updateSession.CreateUpdateInstaller()
                      
                      Set ComputerStatus 	= CreateObject("Microsoft.Update.SystemInfo")
                      Set objShell = CreateObject("WScript.Shell")
                      
                      ' Step 1: Verify if there is a pending reboot, if so: reboot right now.
                      If ComputerStatus.RebootRequired then 
                        WScript.StdOut.Write "This computer needs to reboot before start searching for updates." & vbCrLf
                        if DoReboot then
                          WScript.StdOut.Write "Rebooting in 5 seconds." & vbCrLf
                          strErrorCode = objShell.Run("shutdown.exe -r -f -t 05",0,True)
                        end if
                        WScript.Sleep 3000
                        WScript.Quit 1
                      End if
                      
                      ' Step 2: Search for updates.
                      WScript.StdOut.Write "Wait while searching updates." & vbCrLf
                      Set updateSearch = updateSearcher.Search("IsInstalled=0")
                      If updateSearch.ResultCode <> 2 Then
                        WScript.StdOut.Write "Searching has failed with error code: " & updateSearch.ResultCode & vbCrLf
                        WScript.Sleep 3000
                        WScript.Quit 1
                      End If
                      ' Step 3: If there are new updates download them all.
                      If updateSearch.Updates.Count = 0 Then
                        WScript.StdOut.Write "No new updates. Finishing in 3 seconds." & vbCrLf
                        WScript.Sleep 3000
                        WScript.Quit 2
                      End If
                      
                      WScript.StdOut.Write "Wait while downloading " & updateSearch.Updates.Count & " update(s)." & vbCrLf
                      
                      updateDownloader.Updates = updateSearch.Updates
                      Set downloadResult = updateDownloader.Download()
                      If downloadResult.ResultCode <> 2 Then
                        WScript.StdOut.Write "The download has failed with error code: " & downloadResult.ResultCode & vbCrLf
                        WScript.Sleep 3000
                        WScript.Quit 1
                      End If
                      WScript.StdOut.Write "Download completed." & vbCrLf
                      ' Step 4: Install all downloaded updates.
                      WScript.StdOut.Write "Installing updates ..." & vbCrLf
                      updateInstaller.Updates = updateSearch.Updates
                      Set installationResult = updateInstaller.Install()
                      If installationResult.ResultCode <> 2 Then
                        WScript.StdOut.Write "The installation has failed with error code: " & installationResult.ResultCode & vbCrLf
                        WScript.Sleep 3000
                        WScript.Quit 1
                      End If
                      ' Step 5: Reboot if its needed.
                      If ComputerStatus.RebootRequired then 
                        WScript.StdOut.Write "This computer needs to reboot to complete the installation." & vbCrLf
                        if DoReboot then
                          WScript.StdOut.Write "Rebooting in 5 seconds." & vbCrLf
                          strErrorCode = objShell.Run("shutdown.exe -r -f -t 05",0,True)
                        end if
                        WScript.Sleep 3000
                        WScript.Quit 1
                        Else
                        WScript.StdOut.Write "Script completed." & vbCrLf
                        WScript.Sleep 3000
                        WScript.Quit 2
                      End if
                       

Open in new window


This is a Visual Basic Script that you can use freely. The script has an optional argument to control the reboot behavior of the computer: with /nr the script will not reboot even if it is needed by the update installation to complete. If you run the script without arguments it will always reboot  whenever is needed. If the computer needs to reboot before start to search for updates, it will reboot at the moment you run the script.

To run the script you must use the CSCRIPT.EXE native application. For example:
CScript //Nologo DoUpdate.vbs /nr

Open in new window

You can place the script in a network shared folder with read and execute permissions to everyone (something like the NETLOGON folder on domain controllers) and invoke the script from there. For example:

CScript //Nologo \\FQDN.Domain.Name\NetLogon\DoUpdate.vbs /nr

Open in new window

You can use the same approach when running this script as a scheduled task with the Windows Task Scheduler.

Thanks for reading and good luck to everyone.
1
9,250 Views
Hector2016Systems Administrator and Solutions Architect
CERTIFIED EXPERT

Comments (1)

if we want to run this script for a group of computer only not domain wide ?

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.