Jump to content

Windows Update Agent Async classes (COM)


Recommended Posts

I am attempting to make an auto windows updater to automate a process we do over and over here at my job.

I'm using the Microsoft.Update.Session COM object to search, download, and install updates.

As you can guess downloading and installing possibly hundreds of updates could take a while and my problem is that what I have working right now is synchronous to my script so it pauses which could seem like it's freezing.

So I look at how to do it in an asynchronous fashion, which appears to be possible, but there's one small catch. It wants me to pass it an object that has a method called invoke which then gets passed the progress information. etc etc.

So my problem is that I need to be able to create an object that has a method called invoke in autoit and I have no idea how I'd go about doing that. Is there some sort of com object that is dynamic that I can set a custom method on?

Here's the MSDN reference to the function that takes the "progress event" object.

http://msdn.microsoft.com/en-us/library/aa386132%28v=VS.85%29.aspx

Script so far:

#include <Array.au3>

Dim $arr[1][2]

$test = ObjCreate("Microsoft.Update.Session")

$test2 = $test.CreateUpdateSearcher()

$result = $test2.Search("(IsInstalled=0 and Type='Driver' and IsHidden=0) or (IsInstalled=0 and CategoryIDs contains 'E6CF1350-C01B-414D-A61F-263D14D133B4' and IsHidden=0) or (IsInstalled=0 and CategoryIDs contains '0FA1201D-4330-4FA8-8AE9-B877473B6441' and IsHidden=0)")

ConsoleWrite("Updates: " & $result.Updates.Count & @CRLF)

For $i = 0 To $result.Updates.Count-1
    $update = $result.Updates.Item($i)
    ConsoleWrite(($i + 1) & "> " & $update.Title & @CRLF)
    $i = UBound($arr)
    ReDim $arr[$i+1][2]
    $arr[$i][0] = $update.Title
    $arr[$i][1] = $update.DownloadPriority
Next

_ArraySort($arr)

_ArrayDisplay($arr)

Any ideas are greatly appreciated.

[center]JSON Encoding UDF[/center]

Link to comment
Share on other sites

I can't help you with the subject - because I know nothing about it.

But if I can't find the answer for a problem I have on the forum I usually use Google and search for a solution for visual basic. Because Visual Basic and AutoIt are so similar Google can at least give me a starting point.

So in your case I would search for "IUpdateDownloader BeginDownload visual basic".

After having a look at the BeginDownload function I think you need to register two functions for onprogressChanged and onCompleted using DLLCallbackRegister and then call BeginDownload.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Link to comment
Share on other sites

If you're using Active Directory, you could just install WSUS on the domain controller and set group policy to have it point to your WSUServer. Or if you're not running AD, you can modify local policy to point to a WSUS installation server.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I found a few things that might make this work, but have been unable to make it work myself, perhaps a few of you gents may have better luck with it than I have since I'm not the most experienced with this sort of programming.

I stumbled upon a library called AutoItObject which allows the creation of COM objects and classes in autoit but I've been unable thus far to use it as an object that gets called by the beginsearch method.

http://autoitobject.origo.ethz.ch/

Some other possibly relevant topics:

Here's some of the code I've written:

;==========================================================================================
;   _WUA_Search([$query])
;
;   Remarks:    Searches through updates for the current computer that match the given
;               search criteria.
;   Parameters: $query(Optional) - Search Criteria that will filter out the updates.'
;               $finished(Optional) - A string containing the function name to call when
;                                   the search is finished. If omited then the search will
;                                   be procedural. Function should have 1 parameter which
;                                   will be the search result.
;   Returns:    An object reference to a search result Object.
;==========================================================================================
Func _WUA_Search($query = "IsInstalled=0", $finished = false)
    If $WUA == -1 Then
        $WUA = ObjCreate("Microsoft.Update.Session")
    EndIf
    Local $searcher = $WUA.CreateupdateSearcher()
    If $finished Then
;~      Local $searchCallback = _AutoItObject_Create()
;~      if not _AutoItObject_AddMethod($searchCallback, "Invoke", ) then MsgBox(0, "Error!", "Didn't add method")
        Local $searchCallback = _AutoItObject_Class()
        $searchCallback.Create()
        $searchCallback.AddMethod("Invoke", "_WUA_SearchFinish")
        $searcher.BeginSearch($query, $searchCallback.Object, _WUA_SearchStateClass($searchCallback, $finished))
        if @error Then MsgBox(0, "Error", "Error starting an async search")
    Else
        Return $searcher.Search($query)
    EndIf
EndFunc

Func _WUA_SearchFinish($searchJob, $callbackArgs)
    MsgBox(0, "Done!", "Search Complete!")
    $searcherState = $searchJob.AsyncState
    Call($searcherState.Finished, $searcherState.Searcher.FinishSearch($searchJob))
EndFunc

Func _WUA_SearchStateClass($searcher, $finish)
    Local $oClassObject = _AutoItObject_Class()
    $oClassObject.Create()
    $oClassObject.AddProperty("Searcher", $ELSCOPE_PUBLIC, $searcher)
    $oClassObject.AddProperty("Finished", $ELSCOPE_PUBLIC, $finish)
    Return $oClassObject.Object
EndFunc
Edited by BAM5

[center]JSON Encoding UDF[/center]

Link to comment
Share on other sites

  • 3 years later...

Can you please post what you needed to do to make it work?

Just as a reference for future users having the same problem.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

$searchJob=$searcher.BeginSearch($query, $searchCallback.Object, _WUA_SearchStateClass($searchCallback, $finished))

While String($searchJob.IsCompleted) = "False" ; wait for the search to be complete
    sleep(100)
WEnd
writelogfile("Update search complete")

$searchresult = $searcher.EndSearch($searchJob); finish the search, and store found updates in variable

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...