Friday 11 May 2018

VB Script: Check for pending REBOOT operations in Machine

Sometimes we face scenario like application needs restart before starting the installation as a prerequisite.
Means it checks for pending reboot operations in machine and will restart automatically if detects any. so we can have script to check for any pending reboot operations and proceed with installation only if there is no pending reboot operations.

Script:
    Dim strComputer,strKeyPath1,strKeyPath2,strKeyPath3,strValueName1
Dim strKeyName2,strKeyName3,strValue,SubKey,arrValues1,arrSubKeys2,arrSubKeys3
        Dim strReturn,oReg
        Const HKEY_LOCAL_MACHINE = &H80000002
        Const REG_MULTI_SZ = 7
        strComputer = "."
        strReturn = ""
        Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
       
        strKeyPath1 = "SYSTEM\CurrentControlSet\Control\Session Manager"
        strValueName1 = "PendingFileRenameOperations"
       
        strKeyPath2 = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
        strKeyName2 = "RebootRequired"
       
        strKeyPath3 = "SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing"
        strKeyName3 = "RebootPending"
       
        If oReg.EnumKey (HKEY_LOCAL_MACHINE,strKeyPath2,arrSubKeys2) = 0 Then
         For Each Subkey in arrSubKeys2
          If StrComp(Subkey,strKeyName2) = 0 Then
           strReturn = strReturn & " " & Subkey
          End If
         Next
        End If
       
        oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath1,strValueName1,arrValues1
        If Not IsNull(arrValues1) Then
            strReturn = strReturn & " " & strValueName1
        End If
       
        If oReg.EnumKey (HKEY_LOCAL_MACHINE,strKeyPath3,arrSubKeys3) = 0 Then
         For Each Subkey in arrSubKeys3
          If StrComp(Subkey,strKeyName3) = 0 Then
           strReturn = strReturn & " " & Subkey
          End If
         Next
        End If
       
        If StrComp(strReturn,"") = 0  Then
         Msgbox "No Pending Reboot operations detected!!!"
         'Do your task here
        Else
         Msgbox "Pending Reboot operations detected!!!, Kindly Restart"
        End If
Steps to DO:
1. Copy code and save the script as XXX.vbs
2. Run the script as Administrator.
Will get the below message box if detecting any pending Reboot.


Will get the below message box if not finding any pending Reboot.

No comments:

Post a Comment