Je cherche à automatiser les mises à jour Windows de façon automatique. Après recherche je suis tombé sur cette page qui correspond quasiment à mes attentes :
https://www.autoitscript.com/forum/topi ... -question/
Cependant je souhaite que l'état d'avancement de la recherche, du téléchargement et de l'installation des updates soient visibles dans une GUI et c'est là que je bloque complètement. Je pensais qu'il serait simple d'adapter ce code.
J'ai remplacé les "ConsoleWrite" par des "GUICtrlSetData" pour que les messages s'affichent sur la GUI mais elle fige complètement lorsque le scritp se lance.
► Afficher le texte
Code : Tout sélectionner
#include <GUIConstants.au3>
Opt("GuiOnEventMode", 1)
GUICreate("Windows Update", 300, 200)
$etat = GUICtrlCreateLabel("", 20, 80, 228, 20)
GUISetState(@SW_SHOW)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
; Opt in for the "Microsoft Update" Service, GUID
; ==================================================================================================
GUICtrlSetData($etat, "Enabling Microsoft Update...")
$o_ServiceManager = ObjCreate("Microsoft.Update.ServiceManager")
$o_ServiceManager.ClientApplicationID = "My App"
$o_NewUpdateService = $o_ServiceManager.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"")
; Create the COM-Object for Microsoft-Update
; ==================================================================================================
GUICtrlSetData($etat, "Creating COM-Object for Microsoft Update...")
$o_updateSession = ObjCreate("Microsoft.update.Session")
; Start to search for approved Updates on the Windowsupdate-Source defined
; ==================================================================================================
GUICtrlSetData($etat, "Searching for updates, please wait....")
$o_updateSearcher = $o_updateSession.CreateupdateSearcher()
; Create an object containing the list of applicable Updates...if there are none, exit
; (The packages for Windows Search 4.0, the Office Live-Add-Ins and Windows Live Essentials are
; being excluded due to their impossibly high crappiness factor)
; ==================================================================================================
$o_searchResult = $o_updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
For $i_i = 0 To $o_searchResult.Updates.Count - 1
$s_update = $o_searchResult.Updates.Item($i_i)
Next
If $o_searchResult.Updates.Count = 0 Then
Exit
EndIf
; Create an object list containing the list of Updates to download
; ==================================================================================================
GUICtrlSetData($etat, "Creating collection of updates to download...")
$s_updatesToDownload = ObjCreate("Microsoft.update.UpdateColl")
For $i_i = 0 To $o_searchResult.Updates.Count - 1
$s_update = $o_searchResult.Updates.Item($i_i)
ConsoleWrite($i_i + 1 & "> adding: " & $s_update.Title & @CRLF)
$s_updatesToDownload.Add($s_update)
Next
; Do the download using the object list created above
; ==================================================================================================
;ConsoleWrite(@CRLF & "Downloading updates..." & @CRLF)
GUICtrlSetData($etat, "Téléchargement des mises à jour...")
$o_downloader = $o_updateSession.CreateUpdateDownloader()
$o_downloader.Updates = $s_updatesToDownload
$o_downloader.Download()
ConsoleWrite(@CRLF & "List of downloaded updates:" & @CRLF)
For $i_i = 0 To $o_searchResult.Updates.Count - 1
$s_update = $o_searchResult.Updates.Item($i_i)
If $s_update.IsDownloaded Then
ConsoleWrite($i_i + 1 & "> " & $s_update.Title & @CRLF)
EndIf
Next
; Create an object list containing the list of Updates to install
; ==================================================================================================
;ConsoleWrite(@CRLF & "Creating collection of downloaded updates to install:" & @CRLF & @CRLF)
GUICtrlSetData($etat, "Creating collection of downloaded updates to install...")
$o_updatesToInstall = ObjCreate("Microsoft.update.UpdateColl")
For $i_i = 0 To $o_searchResult.Updates.Count - 1
$s_update = $o_searchResult.Updates.Item($i_i)
If $s_update.IsDownloaded = True Then
ConsoleWrite($i_i + 1 & "> adding: " & $s_update.Title & @CRLF)
$o_updatesToInstall.Add($s_update)
EndIf
Next
; Do the installation using the object list created above
; ==================================================================================================
;ConsoleWrite(@CRLF & "Installing updates..." & @CRLF & @CRLF)
GUICtrlSetData($etat, "Installation des mises à jour...")
$i_installer = $o_updateSession.CreateUpdateInstaller()
$i_installer.Updates = $o_updatesToInstall
If $o_updatesToInstall.Count > 0 Then
$o_installationResult = $i_installer.Install()
; Output results of install
ConsoleWrite("Installation Result: " & $o_installationResult.ResultCode & @CRLF)
ConsoleWrite("Listing of updates installed " & "and individual installation results:" & @CRLF & @CRLF)
For $i_i = 0 To $o_updatesToInstall.Count - 1
ConsoleWrite($i_i + 1 & "> " & $o_updatesToInstall.Item($i_i).Title & ": " & $o_installationResult.GetUpdateResult($i_i).ResultCode & @CRLF)
Next
Else
;ConsoleWrite(@CRLF & "Nothing to install!" & @CRLF & @CRLF)
GUICtrlSetData($etat, "Nothing to install!")
EndIf
; Restart the "Automatic Updates" Service
; ==================================================================================================
ConsoleWrite(@CRLF & "Restarting the Windows Update Service" & @CRLF)
RunWait(@SystemDir & "\net.exe stop wuauserv", "", @SW_HIDE)
sleep(2000)
RunWait(@SystemDir & "\net.exe start wuauserv", "", @SW_HIDE)
; Detect and report the current patchstate to a WSUS server if present
; ==================================================================================================
ConsoleWrite(@CRLF & "Executing wuauctl /detectnow" & @CRLF)
RunWait(@SystemDir & "\wuauctl /detectnow", "", @SW_HIDE)
sleep(2000)
ConsoleWrite(@CRLF & "Executing wuauctl /reportnow" & @CRLF & @CRLF)
RunWait(@SystemDir & "\wuauctl /reportnow", "", @SW_HIDE)
GUICtrlSetData($etat, "Installation terminée.")
Func SpecialEvents()
Select
Case @GUI_CtrlId = $GUI_EVENT_CLOSE
Exit
EndSelect
EndFuncMerci pour votre aide

