none
Patch_Reboot

    Question

  • Option Explicit
    On Error Resume Next

    Dim objShell, objUpdateSession, objUpdateSearcher, objSearcherResults, objUpdate
    Dim objUpdateColl, objInstaller, objInstallResult, objWMIService, objOperatingSystem
    Dim strResultCode
    Dim intShellFail, intCounter1, intCounter2, intCounter3, intIndex1
    Dim colOperatingSystems

    Const EVENTLOG_SUCCESS = 0
    Const EVENTLOG_ERROR = 1
    Const EVENTLOG_WARNING = 2
    Const EVENTLOG_INFORMATION = 4
    Const EVENTLOG_AUDIT_SUCCESS = 8
    Const EVENTLOG_AUDIT_FAILURE = 16

    intCounter1 = 0
    intCounter2 = 0
    intCounter3 = 0

    Err.Clear
    WScript.Echo "Creating wscript shell object..."
    Set objShell = CreateObject("wscript.shell")
    If Err.Number <> "0" Then
    WScript.Echo "*** Unable to create wscript shell object!"
    Set intShellFail = 1
    Else
    WScript.Echo "- Wscript shell object creation is OK!" & vblf
    objShell.LogEvent EVENTLOG_INFORMATION, "WSUSInstallPatchesWithReboot script has been started!" &_
    vblf & "SCRIPT NAME: wsusinstallpatcheswithreboot.vbs" & vblf &_
    "OWNER: Patch Engineering" & vblf & "AUTHORS: Marc Bare"
    End If

    WScript.Echo "Searching for missing approved updates..." & vblf
    Set objUpdateSession = CreateObject("Microsoft.Update.Session")
    Set objUpdateSearcher = objUpdateSession.CreateUpdateSearcher
    Set objSearcherResults = objUpdateSearcher.search("IsInstalled=0")

    WScript.Echo "List of missing approved updates..."
    For intIndex1 = intCounter1 To objSearcherResults.updates.count -1
    Set objUpdate = objSearcherResults.updates.item(intIndex1)
    WScript.Echo intIndex1 + 1 & vbtab & objUpdate.title
    Next

    If intIndex1 = 0 Then
    WScript.Echo "***There are no missing approved updates!" & vblf
    If intShellFail <> 1 Then
    objShell.LogEvent EVENTLOG_INFORMATION, "Automatic Updates located no missing patches!"
    End If
    Else
    If intShellFail <> 1 Then
    objShell.LogEvent EVENTLOG_INFORMATION, "Automatic Updates has detected " &_
    intIndex1 & " missing patches!  Check the" & vblf &_
    "system Log for patch installation status."
    End If
    WScript.Echo vblf & "Updates that will be skipped..."
    Set objUpdateColl = CreateObject("microsoft.update.updatecoll")
    For intIndex1 = intCounter1 To objSearcherResults.updates.count - 1
    Set objUpdate = objSearcherResults.updates.item(intIndex1)
    If objUpdate.isdownloaded = True Then
    If objUpdate.eulaaccepted = False Then
    intCounter2 = intCounter2 + 1
    WScript.Echo intCounter2 & vbtab & objUpdate.title &_
    " will be skipped due to unaccepted EULA" & vblf
    If intShellFail <> 1 Then
    objShell.LogEvent EVENTLOG_WARNING, objUpdate.title & " will NOT be" &_
    vblf & "automatically installed due to unaccepted EULA!"
    End If
    Elseif objUpdate.installationbehavior.canrequestuserinput = True Then
    intCounter2 = intCounter2 + 1
    WScript.Echo intCounter2 & vbtab & objUpdate.title &_
    "will be skipped due to user input requirement" & vblf
    If intShellFail <> 1 Then
    objShell.LogEvent EVENTLOG_WARNING, objUpdate.title & " will NOT be" &_
    vblf & "automatically installed due to user input required!"
    End If
    Else
    objUpdateColl.Add(objUpdate)
    End If
    End If
    Next
    If intCounter2 = 0 Then
    WScript.Echo "***There are no patches that will be skipped!"
    End If
    If objUpdateColl.Count > 0 Then
    WScript.Echo vblf & "Installing updates..."
    Set objInstaller = objUpdateSession.CreateUpdateInstaller()
    objInstaller.updates = objUpdateColl
    Set objInstallResult = objInstaller.Install()
    For intIndex1 = intCounter3 To objUpdateColl.Count - 1
    Select Case objInstallResult.getupdateresult(intIndex1).resultcode
    Case 0
    strResultCode = "Not Started"
    Case 1
    strResultCode = "In Progress"
    Case 2
    strResultCode = "Succeeded"
    Case 3
    strResultCode = "Succeeded With Errors"
    Case 4
    strResultCode = "Failed"
    Case 5
    strResultCode = "Aborted"
    End Select
    WScript.Echo intIndex1 + 1 & vbtab & objUpdateColl.Item(intIndex1).title &_
    vbtab & strResultCode
    Next
    WScript.Echo vblf & "The server should be rebooted to complete patch installation!" & vblf
    End If
    End If

    If intIndex1 = 0 Then
    WScript.Echo "Not rebooting local server due to " & intIndex1 & " missing patches..." & vblf
    else
    WScript.Echo "Rebooting local server..."
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate,(shutdown)}!\\" & "." & "\root\cimv2")
    Set colOperatingSystems = objWMIService.execquery("Select * from Win32_OperatingSystem where Primary=true")
    If Err.Number <> "0" Then
    WScript.Echo "***Unable to query Win32_OperatingSystem class!"
    Else
    For Each objOperatingSystem In colOperatingSystems
    objOperatingSystem.Win32Shutdown(6)
    Next
    End If
    End If

    WScript.Echo "Script execution completed..."

    If intShellFail <> 1 Then
    objShell.LogEvent EVENTLOG_INFORMATION, "WSUSInstallPatchesWithReboot script has completed!" &_
    vblf & "SCRIPT NAME: wsusinstallpatcheswithreboot.vbs" & vblf &_
    "OWNER: Patch Engineering" & vblf & "AUTHORS: Marc Bare"
    End If

    Err.Clear
    WScript.Quit
    Thursday, May 16, 2019 3:27 AM

All replies