Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save priceflex/defc8e461c159979c599364d1108b5a6 to your computer and use it in GitHub Desktop.
Save priceflex/defc8e461c159979c599364d1108b5a6 to your computer and use it in GitHub Desktop.
Windows Update Randomizer
'#############################################################################
'# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment