Created
July 16, 2014 15:50
-
-
Save flakshack/f0a5c11415bf89f9e159 to your computer and use it in GitHub Desktop.
Revisions
-
flakshack created this gist
Jul 16, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,172 @@ '############################################################################# '# Procedure: WUA_SearchDownloadInstall.vbs '# Author: Microsoft/Scott Vintinner '# Last Edit: 07/14/2014 '# Purpose: This script will trigger a Windows Update on this computer '# Notes: Must be run as administrator '# Source: http://msdn.microsoft.com/en-us/library/aa387102%28VS.85%29.aspx '############################################################################# Option Explicit Dim scriptShell, fs Set scriptShell = CreateObject("WScript.Shell") Set fs = CreateObject ("Scripting.FileSystemObject") forceCscript() 'Check if running as wscript and launch cscript '----------LogFile-------------' 'Open the LogFile for writing in same directory as script. Dim logFile, errorCount, backupCount, logFileName errorCount=0 backupCount=0 logFileName = fs.GetParentFolderName(Wscript.ScriptFullName) & "\WUA_SearchDownloadInstall.log" On Error Resume Next Set logFile = fs.CreateTextFile(logFileName) If Err.Number<>0 Then LogEntry("Unable to open the file(s) for writing: " & logFileName) wscript.Quit Else LogEntry("Logfile opened.") End If On Error Goto 0 '----------MSDN Downloader---------' Dim updateSession, updateSearcher, searchResult, I, _ updatesToDownload, update, downloader, downloaderResult, updatesToInstall, _ installer, installationResult Set updateSession = CreateObject("Microsoft.Update.Session") updateSession.ClientApplicationID = "WUA_SearchDownloadInstall.vbs" Set updateSearcher = updateSession.CreateUpdateSearcher() LogEntry "Searching for updates..." & vbCRLF Set searchResult = updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0") LogEntry "List of applicable items on the machine:" For I = 0 To searchResult.Updates.Count-1 Set update = searchResult.Updates.Item(I) LogEntry I + 1 & "> " & update.Title Next If searchResult.Updates.Count = 0 Then LogEntry "There are no applicable updates." WScript.Quit End If LogEntry 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) If update.InstallationBehavior.CanRequestUserInput = true Then LogEntry I + 1 & "> skipping: " & update.Title & " because it requires user input" Else If update.EulaAccepted = false Then update.AcceptEula() End If LogEntry I + 1 & "> adding: " & update.Title updatesToDownload.Add(update) End If Next If updatesToDownload.Count = 0 Then LogEntry "All applicable updates were skipped." WScript.Quit End If LogEntry vbCRLF & "Downloading updates..." Set downloader = updateSession.CreateUpdateDownloader() downloader.Updates = updatesToDownload Set downloaderResult = downloader.Download() LogEntry "Download Result: " & downloaderResult.ResultCode Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl") LogEntry vbCRLF & "Successfully downloaded updates:" For I = 0 To searchResult.Updates.Count-1 set update = searchResult.Updates.Item(I) If update.IsDownloaded = true Then LogEntry I + 1 & "> " & update.Title updatesToInstall.Add(update) End If Next If updatesToInstall.Count = 0 Then LogEntry "No updates were successfully downloaded." WScript.Quit End If LogEntry "Installing updates..." Set installer = updateSession.CreateUpdateInstaller() installer.Updates = updatesToInstall Set installationResult = installer.Install() 'Output results of install LogEntry "Installation Result: " & installationResult.ResultCode LogEntry "Reboot Required: " & installationResult.RebootRequired & vbCRLF LogEntry "Listing of updates installed " & "and individual installation results:" For I = 0 to updatesToInstall.Count - 1 LogEntry I + 1 & "> " & _ updatesToInstall.Item(i).Title & ": " & installationResult.GetUpdateResult(i).ResultCode Next If installationResult.RebootRequired Then RestartSystem() End If '------------- 'RestartSystem '------------- Function RestartSystem() Dim wmi, systems, system, os, OSs 'Use WMI to check if any user is currently logged into the computer Set wmi = GetObject("winmgmts:{impersonationlevel=impersonate}!//./root/CIMV2") Set systems = wmi.ExecQuery("select username from Win32_ComputerSystem") For Each system In systems if system.UserName <> "" Then LogEntry "User is connected:" & system.username & ". Computer will not restart." Exit For Else LogEntry "No user is connected - sending a restart." Set OSs = wmi.ExecQuery("select * from Win32_OperatingSystem") For Each os In OSs os.Reboot() Next End If Next End Function '-------- 'LogEntry '-------- Sub LogEntry(logText) wscript.StdOut.WriteLine(now & ": " & logText) logFile.Write(now & ": " & logText & vbcrlf) End Sub '------------- 'forceCscript '------------- 'This sub will detect if this script was launced with wscript 'then relaunch using vbscript. Sub forceCscript() If Not WScript.FullName = WScript.Path & "\cscript.exe" Then scriptShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO """ & WScript.scriptFullName & """",1,False WScript.Quit 0 End If End Sub