VBS Download en Install Windows Updates

Met behulp van het volgende vbs-script kunnen windows updates gedownload worden en daarna worden deze geinstalleerd en wanneer nodig zal het systeem ook automatisch herstarten. Soms wel makkelijk wanneer je een bepaalde server 's nachts wilt laten updaten.


Set updateSession = CreateObject("Microsoft.Update.Session")
Set updateSearcher = updateSession.CreateupdateSearcher()
Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software'")
Set File = CreateObject("Scripting.FileSystemObject")
Set LogFile = File.OpenTextFile("c:\WUSforceupdate.log", 8, True)
 
LogFile.WriteLine("***************************************************************")
LogFile.WriteLine( "START TIME : " & now)
LogFile.WriteLine( "Searching for updates..." & vbCRLF)
LogFile.WriteLine( "List of applicable items on the machine:")
For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    LogFile.WriteLine( I + 1 & "> " & update.Title)
Next
If searchResult.Updates.Count = 0 Then
    LogFile.WriteLine( "There are no applicable updates.")
    WScript.Quit
End If
LogFile.WriteLine( vbCRLF & "Creating collection of updates to download:")
Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
    updatesToDownload.Add(update)
Next
LogFile.WriteLine( vbCRLF & "Downloading updates...")
Set downloader = updateSession.CreateUpdateDownloader()
    downloader.Updates = updatesToDownload
    downloader.Download()
    LogFile.WriteLine( vbCRLF & "List of downloaded updates:")
For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    If update.IsDownloaded Then
        LogFile.WriteLine( I + 1 & "> " & update.Title )
    End If
Next
Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
LogFile.WriteLine( vbCRLF & "Creating collection of downloaded updates to install:" )
For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        LogFile.WriteLine( I + 1 & "> adding: " & update.Title )
        updatesToInstall.Add(update)
    End If
Next
 
logFile.WriteLine( "Installing updates...")
Set installer = updateSession.CreateUpdateInstaller()
installer.Updates = updatesToInstall
Set installationResult = installer.Install()
'Output results of install
LogFile.WriteLine( "Installation Result: " & installationResult.ResultCode )
LogFile.WriteLine( "Reboot Required: " & installationResult.RebootRequired & vbCRLF )
LogFile.WriteLine( "Listing of updates installed " & "and individual installation results:" )
For I = 0 to updatesToInstall.Count - 1
    LogFile.WriteLine( I + 1 & "> " & updatesToInstall.Item(i).Title & ": " & _
       installationResult.GetUpdateResult(i).ResultCode )
Next
 
if installationResult.RebootRequired = -1 then
    LogFile.WriteLine( "reboot the server")
    strComputer = "."
    Dim refWMIService
    Set objWMIService = GetObject("winmgmts:" & _
        "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer & "\root\cimv2")
    Set colOperatingSystems = objWMIService.ExecQuery ("Select * from Win32_OperatingSystem")
    For Each objOperatingSystem in colOperatingSystems
        ObjOperatingSystem.Reboot()
    Next
end if
 
LogFile.WriteLine( "STOP TIME : " & now)
LogFile.WriteLine("***************************************************************")
LogFile.Close
sult = wusInstaller.Install()