Object error during installation of Windows updates

54 Views Asked by At

My code used to work but now I get:

"The requested action with this object has failed"

The script crashes when I try to install drivers on a new operating system:

Func download_and_install()
    Local $session_install
    Local $hide_stat
    Local $result
    Local $reboot = False
    Local $session_download

    ; Check if update array exists
    If Not IsArray($arr_updates) Then
        log_this("Update array doesn't exist, restarting update phase.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf

    Local $obj_coll
    log_this("Creating update session.")
    Local $obj_sess = ObjCreate("Microsoft.Update.Session", $sys_info[1])
    If @error Then
        log_this("Error creating object, restarting function.")
        Return
    EndIf

    #Region Download updates
    log_this("Creating update downloader.")
    $session_download = $obj_sess.CreateUpdateDownloader()
    If @error Then
        MsgBox(0, "Result", "download error.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf
    log_this("Starting to download updates.")
    $arr_updates[0][1] = 1
    For $c = 1 To $arr_updates[0][0]

        log_this('handling single update download')
        ; Skip if update is not selected and hide if requested
        log_this("hiding update")
        If $arr_updates[$c][3] = 0 Then
            If $prog_update[3][1] = 1 And $prog_update[4][1] = 1 Then
                $hide_stat = $arr_updates[$c][5].IsHidden
                If Not $hide_stat Then $arr_updates[$c][5].IsHidden = True
            EndIf
            ContinueLoop
        EndIf

        sleep(10000)
        log_this("updating data")
        update_install_data()
        log_this("creating coll")
        $obj_coll = ObjCreate("Microsoft.Update.UpdateColl")
        If @error Then
            log_this("Error creating object, restarting function.")
            Return
        EndIf
        log_this("adding update to coll")
        $obj_coll.Add($arr_updates[$c][5])
            If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        log_this("making coll session for download ")
        $session_download.Updates = $obj_coll
        If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        log_this("donwloading session ")
        $session_download.Download
        If @error Then
            MsgBox(0, "Result", "download error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        $arr_updates[0][4] += 1
        log_this("finished download")

    Next
    log_this("Downloading updates finished.")
    #EndRegion Download updates

    #Region Install updates
    log_this("Creating update installer.")
    Local $session_install = $obj_sess.CreateUpdateInstaller()
    If @error Then
        MsgBox(0, "Result", "install error.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
        Return
    EndIf
    log_this("Starting to install updates.")
    $arr_updates[0][1] = 2
    For $d = 1 To $arr_updates[0][0]
        log_this("handling single update install")
        ; Skip if update is not selected
        If $arr_updates[$d][3] <> 1 Then ContinueLoop

        sleep(10000)
        log_this("updating data")
        update_install_data()
        log_this("creating coll")
        $obj_coll = ObjCreate("Microsoft.Update.UpdateColl")
        If @error Then
            log_this("Error while creating object, restarting function.")
            Return
        EndIf
        log_this("adding update to coll")
        $session_install.Updates = $obj_coll
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        log_this("making coll session for install")
        $obj_coll.Add($arr_updates[$d][5])
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        log_this("installing session")
        $result = $session_install.Install
        If @error Then
            MsgBox(0, "Result", "install error.")
            $prog_update[1][1] = 4
            IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
            Return
        EndIf
        If $result.RebootRequired Then $reboot = True
        $arr_updates[0][5] += 1
        log_this("finished")
    Next
    $prog_update[9][1] += $arr_updates[0][5]
    IniWrite($path_progress, "Update", "UpdatesInstalled", $prog_update[9][1])
    log_this("Update installation finished.")
    #EndRegion Install updates

    If $prog_update[3][1] = 1 Then
        $prog_update[1][1] = 7
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
    Else
        log_this("Will check for updates again after reboot.")
        $prog_update[1][1] = 4
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
    EndIf

    If $reboot Then log_this("Reboot required.")
    If $prog_overall[3][1] = "Next" Then allow_exe(1, 0)
    log_this("Rebooting system now.")
    If RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization", "NoLockScreen") = 0 Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\Personalization", "NoLockScreen", "REG_DWORD", 1)
    EndIf
    If IniRead($path_progress, "Overall", "Restart", "") = "Yes" Then
        IniWrite($path_progress, "Overall", "Restart", "No")
    EndIf

    ; Read the current value from the INI file
    Local $restarts = IniRead($path_progress, "AditionalConfig", "Restarts", "0")

    ; Convert the value to an integer and add 1
    If Int($restarts) < 10 Then
        $restarts = Int($restarts) + 1
        IniWrite($path_progress, "AditionalConfig", "Restarts", String($restarts))
        Shutdown(6)
        Exit
    Else
        IniWrite($path_progress, "AditionalConfig", "Restarts", "0")
        $prog_update[1][1] = 7
        IniWrite($path_progress, "Update", "Phase", $prog_update[1][1])
    EndIf
EndFunc   ;==>download_and_install

This is where it crashes but I don't get an error message despite $session_download.Download.

0

There are 0 best solutions below