Jump to content
Sign in to follow this  
doestergaard

Need help with a Windows Update script

Recommended Posts

doestergaard

Hi everyone.

I am currently writing a script for automating Windows Update, converting this PowerShell script into AutoIT: https://social.technet.microsoft.com/Forums/windowsserver/en-US/29448283-0d4f-4bc1-b5e2-59048a348902/windows-update-script-percentage-progress-help?forum=winserverpowershell

Please note, that the working script is at the bottom of the thread above.

Here is my script:

Func WindowsUpdateAgent()
    Local $oUpdateSession, $oUpdateSearcher, $oSearchResults, $oUpdateDownloader, $oUpdateToDownload, $oUpdateInstaller, $oUpdateToInstall
    
    $oUpdateSession = ObjCreate("Microsoft.Update.Session")
    If IsObj($oUpdateSession) Then
    
        ConsoleWrite("Initializing Windows Update...")
        
        $oUpdateSearcher = $oUpdateSession.CreateUpdateSearcher()
        $oSearchResults = $oUpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
        
        If $oSearchResults.Updates.Count = 0 Then Return False
        
        ; Download updates one at a time to output progress
        $oUpdateDownloader = $oUpdateSession.CreateUpdateDownloader()
        
        For $i = 0 To $oSearchResults.Updates.Count -1
            $oUpdate = $oSearchResults.Updates.Item($i)
            
            $oUpdateToDownload = ObjCreate("Microsoft.Update.UpdateColl")
            $oUpdateToDownload.Add($oUpdate)
            
            If $oUpdate.EulaAccepted = False Then $oUpdate.AcceptEula()
            
            ConsoleWrite("Downloading: " & "(" & $i + 1 & "/" & $oSearchResults.Updates.Count & ") '" & $oUpdate.Title & "'")
            
            $oUpdateDownloader.Updates = $oUpdateToDownload
            $oUpdateDownloader.Download()
        Next
        
        ; Install updates one at a time to output progress
        $oUpdateInstaller = $oUpdateSession.CreateUpdateInstaller()
        
        For $i = 0 To $oSearchResults.Updates.Count -1
            $oUpdate = $oSearchResults.Updates.Item($i)
            
            $oUpdateToInstall = ObjCreate("Microsoft.Update.UpdateColl")
            $oUpdateToInstall.Add($oUpdate)
            
            ConsoleWrite("Installing: " & "(" & $i + 1 & "/" & $oSearchResults.Updates.Count & ") '" & $oUpdate.Title & "'")
            
            $oUpdateInstaller.Updates = $oUpdateToInstall
            $oUpdateInstaller.Install()
        Next
        
    Else
        MsgBox($MB_ICONERROR, "Windows Update Agent", "Invalid objecet", 0)
    EndIf
EndFunc ; <== WindowsUpdateAgent()

 

My code already stops when it have fetched the total amount of updated an tries to initiate the download. Then AutoIT throws the error "Must be a type of object", it occurs right after the second ConsoleWrite() - I have compiled the above script, so i can't see what object it fails at.

 

I can't seem to find the error so hope you guys can help me out. Any help is greatly appreciated !

Thanks in advance

Edited by doestergaard

Share this post


Link to post
Share on other sites
JLogan3o13

Can you please look at the output of the console when you get the error? It should tell you which object is causing it to barf. Post a screenshot if necessary.

In essence, if it dies before it prints that ConsoleWrite line, then either $oSearchResults or $oUpdate doesn't exist. If it is printing that line and then errors out, $oUpdateDownloader doesn't exist. Either way, you need to add in error checking; each time an object is (or should be) created, you should be calling IsObj().


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
doestergaard

Im trying to do that right now, the problem is, that it has been running for about an hour now with "Initializing Windows Update", without moving further :o 

Share this post


Link to post
Share on other sites
doestergaard

Okay, apparently the error is gone? But now it runs through approx. 100 updates in a matter of seconds, which is not right :o

Share this post


Link to post
Share on other sites
doestergaard

Okay, i fixed it now.

Here is the code

Func WindowsUpdateAgent()
   Local $oUpdateSession, $oUpdateSearcher, $oUpdateSearchResults, $oUpdateDownloader, $oUpdatesToInstall, $iCounter, $oUpdateToDownload, $oResults, $oUpdateInstaller, $oUpdateToInstall



   $oUpdateSession = ObjCreate("Microsoft.Update.Session")
   If IsObj($oUpdateSession) Then

      ConsoleWrite("Initializing Windows Update..." & @CR)

      $oUpdateSearcher = $oUpdateSession.CreateUpdateSearcher()
      $oUpdateSearchResults = $oUpdateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
      $oUpdatesToInstall = ObjCreate("Microsoft.Update.UpdateColl")

      If $oUpdateSearchResults.Updates.Count = 0 Then

         ConsoleWrite("There is no updates available" & @CR)
      Else
         $oUpdateDownloader = $oUpdateSession.CreateUpdateDownloader()
         $oUpdatesToInstall = ObjCreate("Microsoft.Update.UpdateColl")

         $iCounter = 0
         For $oUpdate In $oUpdateSearchResults.Updates
            $iCounter += 1
            $oUpdateToDownload = ObjCreate("Microsoft.Update.UpdateColl")
            $oUpdateToDownload.Add($oUpdate)
            $oUpdateDownloader.Updates = $oUpdateToDownload



            ConsoleWrite("Downloading: " & "(" & $iCounter & "/" & $oUpdateSearchResults.Updates.Count & ") '" & $oUpdate.Title & "'" & @CR)
            $oResults = $oUpdateDownloader.Download()
            If $oResults.Hresult = 0 And $oResults.ResultCode = 2 Or $oResults.ResultCode = 3 Then

               $oUpdatesToInstall.Add($oUpdate)
            Else
               ConsoleWrite("Failed to download: " & $oUpdate.Title & @CR)
            EndIf

         Next

         ConsoleWrite("-------------------------------------------------" & @CR)

         $oUpdateInstaller = $oUpdateSession.CreateUpdateInstaller()

         $iCounter = 0
         For $oUpdate In $oUpdatesToInstall

            $iCounter += 1
            $oUpdateToInstall = ObjCreate("Microsoft.Update.UpdateColl")
            $oUpdateToInstall.Add($oUpdate)
            $oUpdateInstaller.Updates = $oUpdateToInstall



            ConsoleWrite("Installing: " & "(" & $iCounter & "/" & $oUpdateSearchResults.Updates.Count & ") '" & $oUpdate.Title & "'" & @CR)
            $oUpdateInstaller.Install()
         Next

      EndIf

   Else
      MsgBox($MB_ICONERROR, "Windows Update Agent", "Invalid object", 0)
   EndIf

EndFunc ; <== Main()

I will now try to run it on a complete whiped system to see what happens. I will report back

Share this post


Link to post
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
Sign in to follow this  

×