No announcement yet.

Shutdown and install updates script

  • Filter
  • Time
  • Show
Clear All
new posts

  • Shutdown and install updates script

    Hello all

    Very quick question (and hopefully quick to answer)

    Is it possible to script the 'shutdown and install updates' option?

    I know that you can script an easy shutdown via a batch file i.e.

    shutdown -s -t (time you want to delay shutdown) -f (to force)

    but none of these switches specify to install updates or not

    Will the shutdown -s switch install updates if they need to be installed? or is there something else I need to do?

    Thanks muchly


  • #2
    Re: Shutdown and install updates script

    Hi Rob

    This seems like a straight forward request but after a 20 minute google im stuck. The only thing that came close was this:

    Hope this helps


    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **


    • #3
      Re: Shutdown and install updates script

      AFAIK it is not possible to script a shutdown method that is aware of available updates on the wusserver to be downloaded and installed. And can show a kind of progress of the installation like there is when you manualy shutdown from a session.

      You have do do all the steps one by one in your script.

      Prior to the shutdown, the script must query the WSUS server. check whether for this computer approved and 'not yet installed' updates are available.
      If there are updates they must be downloaded to the computer. And then installed one by one.

      The script must be executed by an account that is memer of the local group Administrators. You can schedule the script on the client computers.
      ' NAME : <wusforceupdate>
      ' AUTHOR : <Samuel lambert>
      ' COMMENT : <Find-Download-Install-reboot (with windows update system WUS)>
      'Shutdown flags;
       const nLog_Off          =  0 
       const nForced_Log_Off   =  4  '( 0 + 4 ) 
       const nShutdown         =  1
       const nForced_Shutdown  =  5  '( 1 + 4 ) 
       const nReboot           =  2
       const nForced_Reboot    =  6  '( 2 + 4 )
       const nPower_Off        =  8
       const nForced_Power_Off = 12  '( 8 + 4 )
      ShutdownOption = nForced_Reboot
      'Monthly log;
      dt = date() : nMonth = Year(dt)*1e2 + Month(dt)
      sLogFile = "C:\WUSforceupdate-" & nMonth & ".log"
      Set updateSession = CreateObject("Microsoft.Update.Session")
      Set updateSearcher = updateSession.CreateupdateSearcher()
      Set searchResult = _
      updateSearcher.Search("IsInstalled=0 and Type='Software'")
      Set File = CreateObject("Scripting.FileSystemObject")
      Set LogFile = File.OpenTextFile(sLogFile, 8, True)
      LogFile.WriteLine( "START TIME : " & now)
      LogFile.WriteLine( "Searching for updates..." & vbCRLF)
      LogFile.WriteLine( "List of applicable items on the machine:")
      For I = 0 To searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        LogFile.WriteLine( I + 1 & "> " & update.Title)
      Set WshShell = WScript.CreateObject("WScript.Shell")
      If searchResult.Updates.Count = 0 Then
        LogFile.WriteLine( "There are no applicable updates.")
        WshShell.popup "There are no applicable updates.",1
        ShutDown(ShutdownOption)  '<-- O P T I O N A L
      End If
      LogFile.WriteLine( 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)
        LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
      LogFile.WriteLine( vbCRLF & "Downloading updates...")
      Set downloader = updateSession.CreateUpdateDownloader() 
      downloader.Updates = updatesToDownload
      LogFile.WriteLine( vbCRLF & "List of downloaded updates:")
      For I = 0 To searchResult.Updates.Count-1
        Set update = searchResult.Updates.Item(I)
        If update.IsDownloaded Then
          LogFile.WriteLine( I + 1 & "> " & update.Title )
        End If
      Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
      LogFile.WriteLine( 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
          'WshShell.popup "installing..." & vbNewLine & update.Title,1
          LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
        End If
      logFile.WriteLine( "Installing updates...")
      Set installer = updateSession.CreateUpdateInstaller()
      installer.Updates = updatesToInstall
      Set installationResult = installer.Install()
      'Output results of install
      LogFile.WriteLine( "Installation Result: " & installationResult.ResultCode )
      LogFile.WriteLine( "Reboot Required: " & installationResult.RebootRequired & vbCRLF )
      LogFile.WriteLine( "Listing of updates installed " _
          & "and individual installation results:" )
      For I = 0 to updatesToInstall.Count - 1
        LogFile.WriteLine( I + 1 & "> " & updatesToInstall.Item(i).Title _ 
          & ": " & installationResult.GetUpdateResult(i).ResultCode ) 
      If installationResult.RebootRequired = -1 then
        LogFile.WriteLine( "RebootRequired")
        ' <-- normally now you should call for a R E B O O T.....
      End if
      ShutDown(ShutdownOption)  '<-- O P T I O N A L
      LogFile.WriteLine( "STOP TIME : " & now)
      wscript.echo "Updates are installed"
      Function ShutDown(sFlag)
       wscript.sleep 600
        Set OScoll = GetObject( _
           "winmgmts:{(Shutdown)}").ExecQuery( _
           "Select * from Win32_OperatingSystem") 
        For Each osObj in OScoll
      End Function

      Similar Thread:

      (more of ExitWindowsEx function
      Last edited by Rems; 5th February 2012, 16:35.

      This posting is provided "AS IS" with no warranties, and confers no rights.


      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts


      • #4
        Re: Shutdown and install updates script

        Thanks Rems. I figured I would have to do something like that

        We are soon to deploy a WSUS server and I thought I'd ask the question before I got too far ahead of myself

        I had a look at another useful link
        which basically goes through the active directory options for WSUS and you can set the default behavior for installed updates there.

        Coupled with this script, I'm sure I'll be able to schedule a 'install updates and shutdown' option as part of our preventative maintenance program here

        Thanks again