Announcement

Collapse
No announcement yet.

Shutdown and install updates script

Collapse
X
  • 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

    Rob

  • #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:

    http://www.wsus.info/forums/index.php?showtopic=6190

    Hope this helps

    Thanks

    Chris
    Server 2000 MCP
    Development: ASP, ASP.Net, PHP, VB, VB.Net, MySQL, MSSQL - Check out my blog http://tonyyeb.blogspot.com

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

    Comment


    • #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.
      Code:
      '
      ' NAME : <wusforceupdate>
      ' AUTHOR : <Samuel lambert>
      ' COMMENT : <Find-Download-Install-reboot (with windows update system WUS)>
      ' www.wsus.info/forums/lofiversion/index.php?t5454.html
      
      '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("***************************************************************")
      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)
      Next
      
      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
        Wscript.quit
      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 )
        updatesToDownload.Add(update)
      Next
      
      LogFile.WriteLine( vbCRLF & "Downloading updates...")
      
      Set downloader = updateSession.CreateUpdateDownloader() 
      downloader.Updates = updatesToDownload
      downloader.Download()
      
      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
      Next
      
      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 )
          updatesToInstall.Add(update) 
        End If
      Next
      
      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 ) 
      Next
      
      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)
      LogFile.WriteLine("***************************************************************")
      LogFile.Close
      
      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
          osObj.Win32Shutdown(sFlag)
        Next
      End Function
      \Rems


      EDIT
      Similar Thread: http://forums.petri.com/showthread.php?t=30580

      tags:
      EWX_LOGOFF , EWX_POWEROFF , EWX_REBOOT , EWX_SHUTDOWN , EWX_FORCE , EWX_FORCEIFHUNG
      (more of ExitWindowsEx function http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx)
      _
      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

      Comment


      • #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 http://www.microsoft.com/technet/com...ip/st0506.mspx
        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

        Comment

        Working...
        X