Blog

Restart PC if pending reboot

This script checks if there is any rename operations pending or updates pending and initiates a restart of the computer if required.
By running this on demand you can ensure that computers are rebooted only if required,minimizing the effects on users and maximising up time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Dim strComputer
Dim strKeyPath
Dim strValueName
Dim strValue
Dim arrValues
Dim bReboot
Dim oReg
Dim objSysInfo
dim objShell
 
On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_MULTI_SZ = 7
strComputer = "."
bReboot = False
 
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
strValueName = "PendingFileRenameOperations"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrValues
 
If Not IsNull(arrValues) Then
  For Each strValue In arrValues
'Pending Rename operations found, reboot
      bReboot = True
      Exit For
  Next
Else
	'No Pending Rename operations
End If
 
'Only Check for update reboots if we need to
If bReboot = False Then
	Set objSysInfo = CreateObject("Microsoft.Update.SystemInfo")
	bReboot = objSysInfo.RebootRequired
End If
 
If bReboot = True then
	strCmd = "shutdown -r -t 120 -f /c " & chr(34) & "A system reboot has been initiated, please save all your work." & chr(34)
 
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strCmd, 0, false 
End If
 
objSysInfo = Nothing
objShell = Nothing

Posted in: Scripts, Technical

Leave a Comment (0) ↓

Leave a Comment