doestergaard Posted July 9, 2015 Share Posted July 9, 2015 (edited) 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=winserverpowershellPlease note, that the working script is at the bottom of the thread above.Here is my script:expandcollapse popupFunc 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 July 9, 2015 by doestergaard Link to comment Share on other sites More sharing options...
Moderators JLogan3o13 Posted July 9, 2015 Moderators Share Posted July 9, 2015 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(). "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum! Link to comment Share on other sites More sharing options...
doestergaard Posted July 9, 2015 Author Share Posted July 9, 2015 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 Link to comment Share on other sites More sharing options...
doestergaard Posted July 9, 2015 Author Share Posted July 9, 2015 Okay, apparently the error is gone? But now it runs through approx. 100 updates in a matter of seconds, which is not right Link to comment Share on other sites More sharing options...
doestergaard Posted July 9, 2015 Author Share Posted July 9, 2015 Okay, i fixed it now.Here is the codeexpandcollapse popupFunc 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now