Jump to content

AutoIT COM: WUA Error


Recommended Posts

Hello all! I am trying to write an AutoIT script that checks for, downloads, and installs Windows Updates.

I have based my code off of Microsoft's VBScripts that are designed to do this. The VBScripts work fine, but the AutoIT script dies when trying to install the updates. I am hoping someone can help me diagnose and get past this error. >_<

My AutoIT Code (See line 87 for where it dies):

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_run_debug_mode=Y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

; ======================================================================================================
; = Updates Search                                                                                     =
; ======================================================================================================
$updateSession = ObjCreate("Microsoft.Update.Session")
$updateSearcher = $updateSession.CreateupdateSearcher()
$updateSearcher.ServerSelection = 2 ; ssWindowsUpdate

ConsoleWrite("Searching for updates..." & @CRLF)

$searchResult = $updateSearcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'")

ConsoleWrite("List of applicable items on the machine:" & @CRLF)

For $I = 0 To $searchResult.Updates.Count-1
     $update = $searchResult.Updates.Item($I)
    ConsoleWrite($I + 1 & "> " & $update.Title & @CRLF)
Next

If $searchResult.Updates.Count = 0 Then
    ConsoleWrite("There are no applicable updates." & @CRLF)
    Exit
EndIf


; ======================================================================================================
; = Updates Collection                                                                                 =
; ======================================================================================================
ConsoleWrite(@CRLF & "Creating collection of updates to download:" & @CRLF)

$updatesToDownload = ObjCreate("Microsoft.Update.UpdateColl")

For $I = 0 to $searchResult.Updates.Count-1
     $update = $searchResult.Updates.Item($I)
    ConsoleWrite($I + 1 & "> adding: " & $update.Title & @CRLF)
    $updatesToDownload.Add($update)
Next


; ======================================================================================================
; = Updates Download                                                                                   =
; ======================================================================================================
ConsoleWrite(@CRLF & "Downloading updates..." & @CRLF)

$downloader = $updateSession.CreateUpdateDownloader() 
$downloader.Updates = $updatesToDownload
$downloader.Download()

ConsoleWrite("List of downloaded updates:" & @CRLF)

For $I = 0 To $searchResult.Updates.Count-1
     $update = $searchResult.Updates.Item($I)
    If $update.IsDownloaded Then
       ConsoleWrite($I + 1 & "> " & $update.Title & @CRLF)
    EndIf
Next

; ======================================================================================================
; = Updates Installation                                                                               =
; ======================================================================================================
$updatesToInstall = ObjCreate("Microsoft.Update.UpdateColl")

ConsoleWrite(@CRLF & "Creating collection of downloaded updates to install:" & @CRLF)

For $I = 0 To $searchResult.Updates.Count-1
     $update = $searchResult.Updates.Item($I)
    If $update.IsDownloaded = 1 Then
       ConsoleWrite($I + 1 & "> adding:  " & $update.Title & @CRLF)
       $updatesToInstall.Add($update)   
    EndIf
Next

ConsoleWrite("Installing updates..." & @CRLF)

; Err Handling
Global $g_eventerror = 0 
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")
Dim $line=0
; Err Handling

$installer = ObjCreate("Microsoft.Update.Installer") ;$updateSession.CreateUpdateInstaller()
$installer.Updates = $updatesToInstall
$installationResult = $installer.Install() ; ##### This is where the script dies, with a COM errror #####


; ======================================================================================================
; = Updates Results                                                                                    =
; ======================================================================================================
ConsoleWrite("Installation Result: " & $installationResult.ResultCode() & @CRLF)
ConsoleWrite("Reboot Required: " & $installationResult.RebootRequired & @CRLF)
ConsoleWrite("Listing of updates installed " & "and individual installation results:"& @CRLF)

For $I = 0 to $updatesToInstall.Count - 1
    ConsoleWrite($I + 1 & "> " & $updatesToInstall.Item($I).Title & ": " & $installationResult.GetUpdateResult($I).ResultCode & @CRLF)
Next


; ======================================================================================================
; = Custom Error Handling Function                                                                     =
; ======================================================================================================
Func MyErrFunc() 
   $HexNumber=hex($oMyError.number,8) 
   ConsoleWrite("== COM ERROR =========================" & @CRLF & _
                "Number is: " & $HexNumber & @CRLF & _
                "Windescription is: " & $oMyError.windescription & @CRLF & _
                "Source is: " & $oMyError.source & @CRLF & _
                "Description is: " & $oMyError.description & @CRLF & _
                "HelpFile is: " & $oMyError.helpfile & @CRLF & _
                "HelpContext is: " & $oMyError.helpcontext & @CRLF & _
                "LastDLLError is: " & $oMyError.lastdllerror & @CRLF & _
                "ScriptLine is: " & $oMyError.scriptline & @CRLF & _
                "======================================" & @CRLF)
   $g_eventerror = 1
Endfunc

Microsoft's Original VBScript code (works without issue):

Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
    WScript.Quit
End If

WScript.Echo 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)
    WScript.Echo I + 1 & "> adding: " & update.Title 
    updatesToDownload.Add(update)
Next

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

WScript.Echo  vbCRLF & "List of downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    If update.IsDownloaded Then
       WScript.Echo I + 1 & "> " & update.Title 
    End If
Next

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

WScript.Echo  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
        WScript.Echo I + 1 & "> adding:  " & update.Title 
       updatesToInstall.Add(update) 
    End If
Next

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "N" or strInput = "n") Then 
    WScript.Quit
ElseIf (strInput = "Y" or strInput = "y") Then
    WScript.Echo "Installing updates..."
    Set installer = updateSession.CreateUpdateInstaller()
    installer.Updates = updatesToInstall
    Set installationResult = installer.Install()
    
    'Output results of install
    WScript.Echo "Installation Result: " & installationResult.ResultCode 
    WScript.Echo "Reboot Required: " & installationResult.RebootRequired & vbCRLF 
    WScript.Echo "Listing of updates installed " & "and individual installation results:" 
    
    For I = 0 to updatesToInstall.Count - 1
        WScript.Echo I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode        
    Next
End If

I first get a COM error - 80020009 (no description) when it hits the install() line. Two subsequent 000000A9 (Variable must be of type 'Object'.) errors appear when trying to show the results of the install. I'm not worried about these last two, as I believe they will clear up once I get the first error resolved.

Can anyone help me to get this working? I have tried it on a Windows XP x86 SP3 machine (missing two updates), and also a Windows XP x64 SP2 machine (missing 13 updates) and both die with the same error on the same line, yet both will run the VBScript fine. Both machines are also running the latest available version of the Windows Update Agent from Microsoft.

Thanks in advance! :(

Link to comment
Share on other sites

Your VBScript is using a function of the "Microsoft.Update.Session" object, where your Autoit is trying to perform this function on the "Microsoft.Update.Installer" object.

This Line:

$installer = ObjCreate("Microsoft.Update.Installer")

Should instead be the one you commented out right after it for it to translate right. Since you commented it out, I take it that it didn't work originally?

Link to comment
Share on other sites

G'day

You could have a look at this thread for a working script

Automatic Windowsupdate / ServicePack question

There are a few others on here but that one is working and should do what you need.

Good Luck

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...