Jump to content

Why this dont work?


Recommended Posts

#include <date.au3>

$updateSession = ObjCreate("Microsoft.update.Session")

$updateSearcher = $updateSession.CreateupdateSearcher()

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

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

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

For $I = 0 To $searchResult.Updates.Count-1

$update = $searchResult.Updates.Item($I)

ConsoleWrite ($I + 1 & "> " & $update.Title & @CR)

Next

If $searchResult.Updates.Count = 0 Then

ConsoleWrite ("There are no applicable updates.")

Exit

EndIf

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

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

For $I = 0 to $searchResult.Updates.Count-1

$update = $searchResult.Updates.Item($I)

ConsoleWrite ($I + 1 & "> adding: " & $update.Title & @CR)

$updatesToDownload.Add($update)

Next

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

$downloader = $updateSession.CreateUpdateDownloader()

$downloader.Updates = $updatesToDownload

$downloader.Download()

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

For $I = 0 To $searchResult.Updates.Count-1

$update = $searchResult.Updates.Item($I)

If $update.IsDownloaded Then

ConsoleWrite ($I + 1 & "> " & $update.Title & @CR)

EndIf

Next

$updatesToInstall = ObjCreate("Microsoft.update.UpdateColl")

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

For $I = 0 To $searchResult.Updates.Count-1

$update = $searchResult.Updates.Item($I)

If $update.IsDownloaded = 1 Then

ConsoleWrite ($I + 1 & "> adding: " & $update.Title & @CR)

$updatesToInstall.Add($update)

EndIf

Next

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

$installer = $updateSession.CreateUpdateInstaller()

$installer.Updates = $updatesToInstall

$installationResult = $installer.Install()

;Output results of install

ConsoleWrite ("Installation Result: " & $installationResult.ResultCode )

ConsoleWrite ("Listing of updates installed " & "and individual installation results:" )

For $I = 0 to $updatesToInstall.Count - 1

ConsoleWrite ($I + 1 & "> " & $updatesToInstall.Item($I).Title & ": " & $installationResult.GetUpdateResult($I).ResultCode & @CR)

Next

why this dont work ? i get a error.

Link to comment
Share on other sites

Works for me now:

#include <date.au3>

$updateSession = ObjCreate("Microsoft.update.Session")
$updateSearcher = $updateSession.CreateupdateSearcher()

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

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

ConsoleWrite("List of applicable items on the machine:")
For $I = 0 To $searchResult.Updates.Count - 1
    $update = $searchResult.Updates.Item($I)
    ConsoleWrite($I + 1 & "> " & $update.Title & @CR)
Next

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

ConsoleWrite(@CRLF & "Creating collection of updates to download:")
$updatesToDownload = ObjCreate("Microsoft.update.UpdateColl")

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

ConsoleWrite(@CRLF & "Downloading updates...")
$downloader = $updateSession.CreateUpdateDownloader()
$downloader.Updates = $updatesToDownload
$downloader.Download()

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

$updatesToInstall = ObjCreate("Microsoft.update.UpdateColl")

ConsoleWrite(@CRLF & "Creating collection of downloaded updates to install:")
For $I = 0 To $searchResult.Updates.Count - 1
    $update = $searchResult.Updates.Item($I)
    If $update.IsDownloaded = True Then
        ConsoleWrite($I + 1 & "> adding: " & $update.Title & @CR)
        $updatesToInstall.Add($update)
    EndIf
Next

ConsoleWrite(@CRLF & "Installing updates...")
$installer = $updateSession.CreateUpdateInstaller()
$installer.Updates = $updatesToInstall
If $updatesToInstall.Count > 0 Then
    $installationResult = $installer.Install()
    ;Output results of install
    ConsoleWrite("Installation Result: " & $installationResult.ResultCode)
    ConsoleWrite("Listing of updates installed " & "and individual installation results:")
    For $I = 0 To $updatesToInstall.Count - 1
        ConsoleWrite($I + 1 & "> " & $updatesToInstall.Item($I).Title & ": " & $installationResult.GetUpdateResult($I).ResultCode & @CR)
    Next
Else
    ConsoleWrite(@CRLF & "Nothing to install" & @CRLF)
EndIf

EDIT: You were not adding to the $updatestoinstall. You would have gotten this same error (even with the fix of 'True' instead of '1') if there were no updates to install. This simple count check pre-install will take care of that issue as well.

Edited by danwilli
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...