;; KixGenerated: 2013/02/17 09:59:25 / Kix32 Version 4.62 
;;KGEN:NOPARSE 
;;HEADER:Lib-WU:1.2:Glenn Barnas:A collection of Windows Update control functions 
;;LIBRARY        Lib-WU - WSUS Control Function Library 
;; 
;;ACTION         A collection of Windows Update control functions 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.2  - 2012/05/01 
;; 
;;HISTORY        1.0  - 2005/09/01 - Initial Release 
;;               1.1  - 2010/01/27 - Minor syntax changes to handle changes to Kix 
;;                                   and the AutoUpdate API. No compatibility issues. 
;;               1.2  - 2012/05/01 - updated wuGetAvailableUpdates and wuDownloadUpdates 
;;                                   UDFs to return a null variable when no updates are found on 
;;                                   the update server. Prior, an empty array was returned which 
;;                                   attempted to enumerate 0 updates rather than reporting 0 updates. 
;;        
;; 
;;OVERVIEW       Collection of functions to manipulate WSUS at the client side via WSUS API. 
; 
; Functions in library: 
; 
; wuCheckUpdateAgent()		Check the version of the update agent - update it if not current 
; wuGetConfig()			Returns an array containing the WSUS configuration 
; wuSetConfig()			Sets the WSUS configuration 
; wuRestartStatus()		Determines the Restart Pending status 
; wuGetAvailableUpdates()	Returns an array of available updates 
; wuDownloadUpdates()		Download and optionally install updates 
; 
 
; Format of array used / returned by the Get/Set WusConfig functions 
;  0	- 0-5 representing the wsus configuration 
;  1	- wsus install day parameter 
;  2	- wsus install time parameter 
;  3	- wsus update server parameter 
;  4	- wsus status server parameter 
;  5	- wsus NoAutoRebootWithLoggedOnUsers parameter 
;  6	- wsus NoAutoUpdate parameter 
;  7	- wsus UseWUServer parameter 
;  8	- wsus DetectionFrequencyEnabled parameter 
;  9	- wsus DetectionFrequency parameter 
;  10	- wsus RebootRelaunchTimeoutEnabled parameter 
;  11	- wsus RebootRelaunchTimeout parameter 
;  12	- wsus RescheduleWaitTimeEnable parameter 
;  13	- wsus RescheduleWaitTime parameter 
; 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuCheckUpdateAgent() 
;; 
;;ACTION         check the version of the update agent - update it if it isn't current 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuCheckUpdateAgent(Version, ExePath) 
;; 
;;PARAMETERS     Version	- REQUIRED, expected version string 
;;		 ExePath	- REQUIRED, path to upgrade executable, with args 
;; 
;;REMARKS         
;; 
;;RETURNS        0 if not available or install/upgrade failed 
;;		 1 if available 
;;		 2 if upgraded / available 
;;		 Exit code is actual result of the upgrade, if processed, otherwise 0 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;;       
; 
Function wuCheckUpdateAgent($_sWUAgentVersion, $_sExePath) 
 
  Dim $_updateSession				; ObjectPointer 
  Dim $_updateinfo				; Returned version string 
 
  $_updateSession = CreateObject("Microsoft.Update.AgentInfo")
  If Not @ERROR
    $_updateinfo = $_updateSession.GetInfo("ProductVersionString")
    If $_updateInfo >= $_sWUAgentVersion
      $wuCheckUpdateAgent = 1
      Exit 0
    EndIf 
  EndIf 
 
  ; stop the Automatic Updates service, installation will restart  
  Shell '%SystemRoot%\system32\net.exe stop wuauserv'
 
  ; install tha AU client  
  Shell $_sExePath
 
  ; handle the install error, if any 
  If @ERROR 
    $wuCheckUpdateAgent = 0			; not available 
    Exit @ERROR 
  EndIf 
 
  $wuCheckUpdateAgent = 2			; upgraded & available 
  Exit 0
 
EndFunction
 
 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuGetConfig() 
;; 
;;ACTION         Returns an array containing the WSUS configuration 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuGetConfig() 
;; 
;;PARAMETERS     none 
;; 
;;REMARKS         
;; 
;;RETURNS        Returns a 14 element array documented above 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;  
Function wuGetConfig()
 
  Dim $_aTmp[13]			; data array 
  Dim $_KeyRoot				; registry root path 
  Dim $_Error				; error flag 
 
  $_Error = 0
  $_KeyRoot = 'HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate'
 
  $_aTmp[0]  = ReadValue($_KeyRoot + '\AU', 'AUOptions')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[1]  = ReadValue($_KeyRoot + '\AU', 'ScheduledInstallDay')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[2]  = ReadValue($_KeyRoot + '\AU', 'ScheduledInstallTime')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[3]  = ReadValue($_KeyRoot, 'WUServer')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[4]  = ReadValue($_KeyRoot, 'WUStatusServer')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[5]  = ReadValue($_KeyRoot + '\AU', 'NoAutoRebootWithLoggedOnUsers')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[6]  = ReadValue($_KeyRoot + '\AU', 'NoAutoUpdate')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[7]  = ReadValue($_KeyRoot + '\AU', 'UseWUServer')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[8]  = ReadValue($_KeyRoot + '\AU', 'DetectionFrequencyEnabled')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[9]  = ReadValue($_KeyRoot + '\AU', 'DetectionFrequency')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[10] = ReadValue($_KeyRoot + '\AU', 'RebootRelaunchTimeoutEnabled')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[11] = ReadValue($_KeyRoot + '\AU', 'RebootRelaunchTimeout')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[12] = ReadValue($_KeyRoot + '\AU', 'RescheduleWaitTimeEnable')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_aTmp[13] = ReadValue($_KeyRoot + '\AU', 'RescheduleWaitTime')
 
  $wuGetConfig = $_aTmp
  Exit $_Error
 
EndFunction
 
 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuSetConfig() 
;; 
;;ACTION         Sets the WSUS configuration 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuSetConfig(array) 
;; 
;;PARAMETERS     array	- REQUIRED, array of values to configure WSUS 
;; 
;;REMARKS         
;; 
;;RETURNS        nothing - exits 1 on error, 0 on success 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;  
Function wuSetConfig($_aConfig)
 
  Dim $_Rc
  Dim $_KeyRoot
  Dim $_Error
 
  $_Error = 0
  $_KeyRoot = 'HKLM\Software\Policies\Microsoft\Windows\WindowsUpdate'
 
  $_Rc = WriteValue($_KeyRoot + '\AU', 'AUOptions', $_aConfig[0], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'ScheduledInstallDay', $_aConfig[1], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'ScheduledInstallTime', $_aConfig[2], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot, 'WUServer', $_aConfig[3], 'REG_SZ')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot, 'WUStatusServer', $_aConfig[4], 'REG_SZ')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'NoAutoRebootWithLoggedOnUsers', $_aConfig[5], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'NoAutoUpdate', $_aConfig[6], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'UseWUServer', $_aConfig[7], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'DetectionFrequencyEnabled', $_aConfig[8], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'DetectionFrequency', $_aConfig[9], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'RebootRelaunchTimeoutEnabled', $_aConfig[10], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'RebootRelaunchTimeout', $_aConfig[11], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'RescheduleWaitTimeEnable', $_aConfig[12], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
  $_Rc = WriteValue($_KeyRoot + '\AU', 'RescheduleWaitTime', $_aConfig[13], 'REG_DWORD')
  $_Error = IIf(@ERROR, 1, $_Error)
 
  Exit $_Error
 
EndFunction
 
 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuRestartStatus() 
;; 
;;ACTION         Determines the Restart Pending status 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuRestartStatus() 
;; 
;;PARAMETERS     None 
;; 
;;REMARKS        A pending restart can prevent additional updates from being 
;;		 applied. This function should be called, and the system  
;;		 restarted if needed, prior to attempting additional updates. 
;; 
;;RETURNS        Boolean indicating whether a restart is pending 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;  
Function wuRestartStatus()
 
  Dim $_ComputerStatus				; object pointer 
 
  $_ComputerStatus = CreateObject("Microsoft.Update.SystemInfo")
  $wuRestartStatus = $_ComputerStatus.RebootRequired
  Exit @ERROR
 
EndFunction
 
 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuGetAvailableUpdates() 
;; 
;;ACTION         Returns an array of available updates 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuGetAvailableUpdates() 
;; 
;;PARAMETERS     none 
;; 
;;REMARKS        Returns the list of update descriptions 
;; 
;;RETURNS        Array 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
; 
Function wuGetAvailableUpdates()
 
  Dim $_updateSession				; Object POinter 
  Dim $_updateSearcher				; search session 
  Dim $_SearchResult				; result array 
  Dim $_aTmp					; return array 
  Dim $_I					; index pointer 
  Dim $_Update					; Index handle 
 
  $_updateSession = CreateObject("Microsoft.Update.Session")
  $_updateSearcher = $_updateSession.CreateupdateSearcher()
 
  $_SearchResult = $_updateSearcher.Search("IsInstalled=0 and Type='Software'")
  
  If $_SearchResult.Updates.Count > 0
    For $_I = 0 to ($_SearchResult.Updates.Count - 1)
      ReDim Preserve $_aTmp[$_I]
      $_Update = $_SearchResult.Updates.Item($_I)
      $_aTmp[$_I] = $_Update.Title
    Next
    ; return array  
    $wuGetAvailableUpdates = $_aTmp
  EndIf
 
  Exit 0
 
EndFunction
 
 
 
;; 
;;====================================================================== 
;; 
;;FUNCTION       wuDownloadUpdates() 
;; 
;;ACTION         Download and optionally install updates 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;SYNTAX         wuDownloadUpdates(Install) 
;; 
;;PARAMETERS     Install	OPTIONAL, install downloaded updates 
;; 
;;REMARKS         
;; 
;;RETURNS        Array of downloaded updates or Array of installed updates and status 
;;		 If no downloads/updates are available, array returns: 
;;			not set		 - Boolean indicating no updates available 
;;		 If list downloads only, array returns: 
;;			0  : 0		 - Boolean indicating no install 
;;			1+ : string      - Remaining entries list the downloaded update titles 
;;		 If installation is performed, array returns: 
;;			0  : 1		 - Boolean indicating install was performed 
;;			1  : n		 - Install result code 
;;			2  : b		 - Boolean indicating reboot is required 
;;			3+ : string	 - Remaining entries list the installed update titles 
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K, WXP, W2K3 
;; 
;;EXAMPLES        
; 
Function wuDownloadUpdates(OPTIONAL $_wuInstall)
 
  Dim $_Rc					; Return Code catcher 
  Dim $_I					; Index var 
  Dim $_aTmp					; Data array 
  Dim $_C					; Count 
  Dim $_Update					; Update handle 
  Dim $_Installer				; Installer handle 
  Dim $_UpdateSession				; Collection 
  Dim $_UpdateSearcher				; Collection 
  Dim $_UpdatesToDownload			; Collection 
  Dim $_UpdatesToInstall			; Collection 
  Dim $_SearchResult				; Collection 
  Dim $_Downloader				; Collection 
  Dim $_InstallationResult			; Status 
 
  ; Creating collection of updates to download 
  $_UpdateSession  = CreateObject("Microsoft.Update.Session")
  $_UpdateSearcher = $_UpdateSession.CreateupdateSearcher()
  $_searchResult   = $_UpdateSearcher.Search("IsInstalled=0 and Type='Software'")
 
  $_UpdatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
 
  ; no available updates 
  If $_SearchResult.Updates.Count = 0
    $wuDownloadUpdates = ''
    Exit 0
  EndIf
 
  ; build list of updates 
  For $_I = 0 to $_SearchResult.Updates.Count - 1
    $_Update = $_SearchResult.Updates.Item($_I)
    $_Rc = $_UpdatesToDownload.Add($_Update)
  Next
 
  ; Download updates 
  $_Downloader = $_UpdateSession.CreateUpdateDownloader() 
  $_Downloader.Updates = $_UpdatesToDownload
  $_Rc = $_Downloader.Download()
 
  ; build list of downloaded updates in $_aTmp 
  $_C = $_searchResult.Updates.Count
  If $_C > 0
    ReDim $_aTmp[$_C]
    $_aTmp[0] = 0
    For $_I = 0 To $_C - 1
      $_Update = $_searchResult.Updates.Item($_I)
      If $_Update.IsDownloaded
        $_aTmp[$_I + 1] = $_Update.Title
      EndIf
    Next
  EndIf
 
  ; should we install? 
  If $_wuInstall
    ; Installing updates... 
    $_UpdatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
 
    ; create collection to install 
    For $_I = 0 To $_searchResult.Updates.Count - 1
      $_Update = $_searchResult.Updates.Item($_I)
      If $_Update.IsDownloaded
        $_Rc = $_UpdatesToInstall.Add($_Update)
      EndIf
    Next
 
    $_Installer = $_UpdateSession.CreateUpdateInstaller()
    $_Installer.Updates = $_UpdatesToInstall
    $_InstallationResult = $_Installer.Install()
 
    ; Output results of install 
    $_C = $_UpdatesToInstall.Count
    ReDim $_aTmp[$_C + 2]
    $_aTmp[0] = 1
    $_aTmp[1] = $_InstallationResult.ResultCode
    $_aTmp[2] = $_InstallationResult.RebootRequired
    For $_I = 0 to $_C - 1
      $_aTmp[$_I + 3] = $_UpdatesToInstall.Item($_I).Title + ':' + $_InstallationResult.GetUpdateResult($_I).ResultCode 		
    Next
  EndIf
 
  $wuDownloadUpdates = $_aTmp
  Exit 0
 
EndFunction
 
 
 
;======================================================================================== 
;                              END OF WINDOWS UPDATE LIBRARY 
;========================================================================================