Sign in to follow this  
Followers 0
cammn

Call and continue an ObjCreate

4 posts in this topic

Many of you know the Windows Update API. The problem I'm trying to work out is this (applies to other API's too I'm sure).

I call an ObjCreate ($objUpdateSession = ObjCreate('Microsoft.update.Session'))

But when I get to the actual install of updates ($installationResult = $installer.Install()) it sits on this one line until the updates are installed. How can I call this and move on.

I'd like to probe the progress and display it. This also applies to the download portion. So far my only thought was to FileInstall a second script but I'm hoping there's a better way.

Excuse the example, it's just a chuck of a much larger script but should give you the pricipal.

Thanks for any ideas.

Func _WinUpdates($sPatch_SP) ; Decides what options are selected and branches off
Local $downloader, $objUpdatesToInstall, $sResultCode
Local $installer, $installationResult
Dim $tempDownload

; MS Ref: http://msdn2.microsoft.com/en-us/library/aa387291(VS.85).aspx

; >> Start << collecting update information about the local machine needed for the rest of the functions.
            $objUpdateSession = ObjCreate('Microsoft.update.Session')
            $objUpdateSearcher = $objUpdateSession.CreateupdateSearcher()

 ; Searching for updates...
             _WriteLog('Searching for available updates')
             _SplashTrayTips ('Searching for updates');, 1, 17)
             $objSearchResult = $objUpdateSearcher.Search('IsInstalled=0 and Type=''Software''')

 ; List of applicable items on the machine:
            For $I = 0 To $objSearchResult.Updates.Count-1
                 $objUpdate = $objSearchResult.Updates.Item($I)
                 _WriteLog ('Available: ' & $objUpdate.Title & @TAB & 'KB date: ' & StringTrimRight ($objUpdate.LastDeploymentChangeTime, 6) & @Tab & 'MSRC: ' & $objUpdate.MsrcSeverity)
                    If $objUpdate.EulaAccepted = False Then
                        _Writelog ('Accepted EULA for ' & $objUpdate.title)
                        $objUpdate.AcceptEula
                    EndIf
            Next
; >> End << collecting update information about the local machine needed for the rest of the functions.

; If no updates are found
        If $objSearchResult.Updates.Count = 0 Then
            _WriteLog('There are no applicable updates, exiting script')
            SplashOff ()
            If $bSplashTips or $bTrayTips and _
                RegRead (_GetRegKey ('HKLM')  & '\Software\Arrow\WindowsUpdate', 'BeenRebooted') = '' Then MsgBox (0, 'Automatic Update', 'There are no applicable updates')
            _Exit (False)
        EndIf

;                   >>>>>>>> Start branching off options <<<<<<<<<<<

; Requested to unhide updates ; Do this before installing updates so the hidden are available
        If StringInStr ($appCMDParams, 'UnhideCritical') or StringInStr ($appCMDParams, 'UnhideAll') Then _UnhideHidden()

; So they want service packs installed.  Find the right package for the OS
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_XP' Then _WU_XP_SP($objSearchResult)
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_Vista' and _GetOSServicePack() = '' Then _WU_Vista_RTM($objSearchResult)
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_Vista' and _GetOSServicePack() = 'Service Pack 1' Then _WU_Vista_SP1($objSearchResult)
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_Vista' and _GetOSServicePack() = 'Service Pack 2' Then _WU_Post_SP($objSearchResult)
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_7' and _GetOSServicePack() = '' Then _WU_7_RTM($objSearchResult)
        If $sPatch_SP = 'SP' And _GetOSVersion() = 'Win_7' and _GetOSServicePack() = 'Service Pack 1' Then _WU_Post_SP($objSearchResult)

; Request specific KB(s) to be installed
        If StringInStr ($appCMDParams, 'KB=') Then _WU_Patch($objSearchResult)

; Request specific days back to install
        If StringInStr ($appCMDParams, 'Days=') Then _WU_Patch($objSearchResult)

; Request to patch critical  without service packs ; If "Days" is selected, this isn't applicable as it will install all critical
        If $sPatch_SP = 'Patch' and StringInStr ($appCMDParams, 'Days=') = False Then _WU_Patch($objSearchResult)

; If no above criteria matches we just run with it.  Put here due to Windows Vista SP not installing updates past SP2
;       _WU_Patch($objSearchResult)

;                   >>>>>>>>> End branching off options <<<<<<<<<<<<


; ============================ DOWNLOAD AND INSTALL ========================
;#cs    actual download / install start ; rem out to endfunc to stop the updates from downloading / installing in test
; Other functions gathered updates needed and we download / install them
            _WriteLog('1 - Downloading updates')
            ;_SplashTrayTips ('1 - Downloading ' & $objUpdatesToDownload.Count & ' update');, 1, 17)
            If $bTrayTips = True And $objUpdatesToDownload.Count = 1 Then _SplashTrayTips ('Downloading ' & $objUpdatesToDownload.Count & ' update')
            _SplashTrayTips ('Downloading ' & $objUpdatesToDownload.Count & ' update(s)');, 1, 17)
;ConsoleWrite ('$objUpdateSession.CreateUpdateDownloader' & @ScriptLineNumber & @CRLF)
            $downloader = $objUpdateSession.CreateUpdateDownloader()
;ConsoleWrite ('$downloader.Updates' & @ScriptLineNumber & @CRLF)
            $downloader.Updates = $objUpdatesToDownload
;ConsoleWrite ('$downloader.Download' & @ScriptLineNumber & @CRLF)
            $downloader.Download()  ; ----------------------------------------- Download

; List of downloaded updates:
            For $I = 0 To $objSearchResult.Updates.Count-1
                 $objUpdate = $objSearchResult.Updates.Item($I)
                If $objUpdate.IsDownloaded Then
                   _WriteLog('Downloaded update: ' & $objUpdate.Title)
                   ;_SplashTrayTips ('Already downloaded: ' & $objUpdate.Title);, 1, 17)
                   ;ConsoleWrite ($I + 1 & "> ' & $objUpdate.Title & @CR)
                EndIf
            Next

         $objUpdatesToInstall = ObjCreate('Microsoft.update.UpdateColl')

; Creating collection of downloaded updates to install:
            _WriteLog('Creating collection of downloaded updates to install')
            _SplashTrayTips ('Creating collection of downloaded updates to install');, 5, 17)
            For $I = 0 To $objSearchResult.Updates.Count-1
                 $objUpdate = $objSearchResult.Updates.Item($I)
                If $objUpdate.IsDownloaded Then
                   ;ConsoleWrite ($I + 1 & "> adding:  ' & $objUpdate.Title & @CR)
                   _WriteLog('Installing update: ' & $objUpdate.Title)
                   _SplashTrayTips ('Installing update(s)');: '  & $objUpdate.Title);, 1, 17);  : ' & $objUpdate.Title, 1, 17)
                   $objUpdatesToInstall.Add($objUpdate)
                EndIf
            Next

; Installing updates...
                $installer = $objUpdateSession.CreateUpdateInstaller()
            ;_WriteLog('Pre Updates: ' & $objUpdate.Title)
                $installer.Updates = $objUpdatesToInstall

            ;_WriteLog('Pre EULA: ' & $objUpdate.Title)
            If $objUpdate.EulaAccepted = False Then $objUpdate.AcceptEula

                $installationResult = $installer.Install()      ;-------------------------- Install

            For $I = 0 to $objUpdatesToInstall.Count - 1
                Switch $installationResult.GetUpdateResult($I).ResultCode
                    Case 0
                        $sResultCode = 'Not Started'
                    Case 1
                        $sResultCode = 'In Progress'
                    Case 2
                        $sResultCode = 'Succeeded'
                    Case 3
                        $sResultCode = 'Succeeded With Errors'
                    Case 4
                        $sResultCode = 'ERROR: Failed'
                        $bErrorFlag = True
                        $sErrorLog = $sErrorLog & 'ERROR: ' & $objUpdatesToInstall.Item($I).Title & @Tab
                    Case 5
                        $sResultCode = 'Aborted'
                EndSwitch
                _WriteLog('Installed update: ' & $objUpdatesToInstall.Item($I).Title & @TAB & ' Result code: ' & $sResultCode);$installationResult.GetUpdateResult($I).ResultCode)
                ;orcNotStarted= 0,  orcInProgress= 1,  orcSucceeded= 2,  orcSucceededWithErrors= 3,  orcFailed= 4,  orcAborted= 5

                ;_SplashTrayTips ('Installed update: ' & $objUpdatesToInstall.Item($I).Title)
            Next
            _Exit (True)
            ;If $bRebootRequired = True Then _Exit (True)
;#ce ; Actual install end
EndFunc ; _WinUpdates()

Share this post


Link to post
Share on other sites



Hi, maybe try change:

$installationResult = $installer.Install()

to

$installationResult = $installer.BeginInstall()

Share this post


Link to post
Share on other sites

Hi, maybe try change:

$installationResult = $installer.Install()

to

$installationResult = $installer.BeginInstall()

Yep! Thank you very much for that. I must have missed it with all the glaring over in MSDN options for WinUpdate.

Share this post


Link to post
Share on other sites

No problem! So just curious, how do you plan on monitoring the installs? By COM or reading the install window? If you do it using the object, do you mind if I see the code you come up with?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0