Code : Tout sélectionner
#RequireAdmin
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <Misc.au3>
#include <MsgBoxConstants.au3>
If Not _Singleton(@ScriptName) Then Exit
Global $ColNeeded
Global $Host = @ComputerName
$BannedList = StringSplit("Essentials|Silverlight|malveillants|Bing|Outil", "|")
$Gui = GUICreate("MAJ Script", 800, 600)
GUISetBkColor(0xb2ccff, $Gui)
$NeededListView = GUICtrlCreateListView("Noms|Status", 10, 40, 780, 450, $LVS_SHOWSELALWAYS)
_GUICtrlListView_SetColumnWidth($NeededListView, 0, 657)
_GUICtrlListView_SetColumnWidth($NeededListView, 1, 100)
$UpdatesFound = GUICtrlCreateLabel("", 10, 10, 200, 20)
GUICtrlSetBkColor($UpdatesFound, "0xffffff")
$Auto = GUICtrlCreateButton("Auto", 690, 10, 100, 20)
$AutoRunCountdown = GUICtrlCreateLabel("10 Secondes avant le commencement !", 250, 10, 280, 20)
GUICtrlSetFont(-1, 13)
GUISetState()
$InitTime = TimerInit()
AdlibRegister("_AutoRunDelay", 100)
Do
Sleep(50)
$MSG = GUIGetMsg()
If $MSG == $GUI_EVENT_CLOSE Then Exit
If $MSG == $Auto Then
AdlibUnRegister()
GUICtrlSetData($AutoRunCountdown, "")
_PopulateNeeded($Host)
EndIf
Until True == False
Func _AutoRunDelay()
$TimeLeft = 10 - (TimerDiff($InitTime) / 1000)
GUICtrlSetData($AutoRunCountdown, Int($TimeLeft) & " Secondes avant le commencement !")
If $TimeLeft < 0 Then
GUICtrlSetData($AutoRunCountdown, "")
_PopulateNeeded($Host)
EndIf
EndFunc ;==>_AutoRunDelay
Func _CreateMSUpdateSession($strHost = @ComputerName)
$objSession = ObjCreate("Microsoft.Update.Session", $strHost)
If Not IsObj($objSession) Then Return 0
Return $objSession
EndFunc ;==>_CreateMSUpdateSession
Func _CreateSearcher($objSession)
If Not IsObj($objSession) Then Return -1
Return $objSession.CreateUpdateSearcher
EndFunc ;==>_CreateSearcher
Func _FetchNeededData($Host)
$objSearcher = _CreateSearcher(_CreateMSUpdateSession($Host))
$ColNeeded = _GetNeededUpdates($objSearcher)
$objSearcher = 0
Dim $arrNeeded[1][2]
For $i = 0 To $ColNeeded.Updates.Count - 1
If $i < $ColNeeded.Updates.Count - 1 Then ReDim $arrNeeded[$i + 2][2]
$update = $ColNeeded.Updates.Item($i)
$arrNeeded[$i][0] = $update.Title
$arrNeeded[$i][1] = $update.Description
Next
If Not IsArray($arrNeeded) Then
MsgBox(16, "Windows Updates Service semble avoir rencontré un problème avec:" & $Host, "", 5)
Return 0
EndIf
Return $arrNeeded
EndFunc ;==>_FetchNeededData
Func _GetNeededUpdates($objSearcher)
If Not IsObj($objSearcher) Then Return -5
$ColNeeded = $objSearcher.Search("IsInstalled=0 and Type='Software'")
Return $ColNeeded
EndFunc ;==>_GetNeededUpdates
Func _GetTotalHistoryCount($objSearcher)
If Not IsObj($objSearcher) Then Return -2
Return $objSearcher.GetTotalHistoryCount
EndFunc ;==>_GetTotalHistoryCount
Func _PopulateNeeded($Host)
AdlibUnRegister()
_GUICtrlListView_DeleteAllItems(ControlGetHandle($Gui, "", $NeededListView))
SplashTextOn("MAJ Script", "Recherche en cours..", 400, 30, -1, -1, 32)
$arrNeeded = _FetchNeededData($Host)
SplashOff()
If IsArray($arrNeeded) And $arrNeeded[0][0] <> "" Then
For $i = 0 To UBound($arrNeeded) - 1
_GUICtrlListView_AddItem($NeededListView, $arrNeeded[$i][0])
$Dirty = False
For $Check = 1 To $BannedList[0]
If StringInStr($arrNeeded[$i][0], $BannedList[$Check]) Then $Dirty = True
Next
If $Dirty == False Then _GUICtrlListView_SetItemSelected($NeededListView, $i, True)
Next
Else
_GUICtrlListView_AddItem($NeededListView, "Aucune mises à jour trouvée!")
EndIf
$objSearcher = 0
$arrNeeded = 0
AdlibRegister("_KillRestartWindow")
_UpdatesDownloadAndInstall()
EndFunc ;==>_PopulateNeeded
Func _KillRestartWindow()
If WinExists("MAJ Script", "") Then
WinActivate("MAJ Script", "")
WinClose("MAJ Script", "")
EndIf
EndFunc ;==>_KillRestartWindow
Func _UpdatesDownloadAndInstall()
GUICtrlSetData($AutoRunCountdown, "")
$Selected = _GUICtrlListView_GetSelectedIndices($NeededListView, True)
If $Selected[0] = 0 Then
AdlibUnRegister()
MsgBox(0,@ScriptName,"Aucune mises à jour restantes",5)
Exit
EndIf
$objSearcher = _CreateMSUpdateSession($Host)
For $x = 1 To $Selected[0]
$item = _GUICtrlListView_GetItemText($NeededListView, $Selected[$x])
For $i = 0 To $ColNeeded.Updates.Count - 1
$update = $ColNeeded.Updates.Item($i)
If $item = $update.Title Then
GUICtrlSetData($UpdatesFound, "Téléchargement de " & $x & " à " & $Selected[0] & " Updates")
_GUICtrlListView_SetItemText($NeededListView, $i, "Téléchargement...", 1)
_GUICtrlListView_SetItemFocused($NeededListView, $i)
_GUICtrlListView_EnsureVisible($NeededListView, $i)
$updatesToDownload = ObjCreate("Microsoft.Update.UpdateColl")
$updatesToDownload.Add($update)
$DownloadSession = $objSearcher.CreateUpdateDownloader()
$DownloadSession.Updates = $updatesToDownload
$DownloadSession.Download
_GUICtrlListView_SetItemText($NeededListView, $i, "Téléchargé", 1)
EndIf
Next
Next
$RebootNeeded = False
For $x = 1 To $Selected[0]
$item = _GUICtrlListView_GetItemText($NeededListView, $Selected[$x])
For $i = 0 To $ColNeeded.Updates.Count - 1
$update = $ColNeeded.Updates.Item($i)
If $item = $update.Title And $update.IsDownloaded Then
GUICtrlSetData($UpdatesFound, "Installation de " & $x & " sur " & $Selected[0] & " Updates")
_GUICtrlListView_SetItemText($NeededListView, $i, "Installation...", 1)
_GUICtrlListView_SetItemFocused($NeededListView, $i)
_GUICtrlListView_EnsureVisible($NeededListView, $i)
$InstallSession = $objSearcher.CreateUpdateInstaller()
$updatesToInstall = ObjCreate("Microsoft.Update.UpdateColl")
$updatesToInstall.Add($update)
$InstallSession.Updates = $updatesToInstall
$installresult = $InstallSession.Install
If $installresult.RebootRequired Then
$RebootNeeded = True
EndIf
_GUICtrlListView_SetItemText($NeededListView, $i, "Accomplie", 1)
EndIf
Next
Next
If $RebootNeeded Then
MsgBox(0, "MAJ Script", "Un redémarrage est nécessaire. donc je redémarre dans 5 secondes.", 5)
Shutdown(2 + 4 + 16)
Else
_GUICtrlListView_DeleteAllItems($NeededListView)
_PopulateNeeded($Host)
EndIf
$DownloadSession = 0
$updatesToDownload = 0
Return 0
EndIf
EndFunc ;==>_UpdatesDownloadAndInstall