Powershell WUA script hangs on calling Update() and Install() RRS feed

  • Question

  • I am trying to write a script that will use the WUA API to install certain updates. I have written many Powershell scripts, but this is my first time using COM objects, or interacting with the WUA API. I have used the code from this article: http://msdn.microsoft.com/en-us/library/windows/desktop/aa387102(v=vs.85).aspx I added in my own code to filter the available updates to a list of updates that I want to install. Everything is working fine, except the script hangs after the calls to both Download() and Install().

    When I call Download(), the updates are successfully downloaded, but the script will not continue on. The cursor just sits there blinking. If I monitor WindowsUpdate.log, waiting until the updates are finished downloading, return to the Powershell window, and press the <Enter> key, the script continues on. The same happens with my call to Install().

    Here are the sections of code at issue:

    'Downloading updates...'
    $downloader = $UpdateSession.CreateUpdateDownloader()
    $downloader.Updates = $updatesToDownload


    "Installing Updates..."
    $Installer = $UpdateSession.CreateUpdateInstaller()
    $Installer.Updates = $updatesToInstall
    $results = $Installer.Install()

    In neither case is the word "Done" displayed until after I hit <Enter>. The rest of the code is just as it is on the MSDN page linked above (the Powershell version). Why is this happening?

    Thursday, March 6, 2014 2:16 AM


All replies

  • You failed to  post how you created the update object - $UpdateSession


    Thursday, March 6, 2014 3:16 AM
    • Marked as answer by Bill_Stewart Saturday, July 5, 2014 2:58 PM
    Thursday, March 6, 2014 3:22 AM
  • The $UpdateSession was created as follows:

    $UpdateSession = New-Object -ComObject 'Microsoft.Update.Session'
    $UpdateSession.ClientApplicationID = 'Company Update Script'

    Friday, March 7, 2014 6:34 PM
  • I think I have found the solution to my problem.

    I changed this line:


    to this:

    $dresult = $downloader.Download()

    Now the script runs. Apparently, if I try to ignore the return value of that function it clobbers something. I am used to just ignoring return values when I don't want them. (I know that ignoring return values is not a good practice, but this script is not intended to be robust. It will only be used occasionally, and only by me.)

    Thank you for the link, jrv. It got me going in the right direction.

    Friday, March 7, 2014 10:01 PM