-
Notifications
You must be signed in to change notification settings - Fork 15
/
Check-RebootPending.ps1
54 lines (47 loc) · 2.93 KB
/
Check-RebootPending.ps1
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
47
48
49
50
51
52
53
54
# $ComponentBasedServicing = (Get-ChildItem 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\').Name.Split("\") -contains "RebootPending"
# $WindowsUpdate = (Get-ChildItem 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\').Name.Split("\") -contains "RebootRequired"
# $PendingFileRename = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').PendingFileRenameOperations.Length -gt 0
# $ActiveComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName
# $PendingComputerName = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
# $PendingComputerRename = ($ActiveComputerName -ne $PendingComputerName)
Function Check-RebootPending
{
Param ([string]$ComputerName)
Try {
[boolean]$rebootRequired = $false
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
Try {
# Check for CBS reboots...
$regKey = $reg.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing')
If ($regKey) { ForEach ($regVal In $regKey) { If ($regVal -contains 'RebootPending') { $rebootRequired = $true } } }
Try { $regKey.Close() } Catch { }
} Catch { }
Try {
# Check for Windows update reboots...
$regKey = $reg.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update')
If ($regKey) { ForEach ($regVal In $regKey) { If ($regVal -contains 'RebootRequired') { $rebootRequired = $true } } }
Try { $regKey.Close() } Catch { }
} Catch { }
Try {
# Check for session manager updates...
$regKey = $reg.OpenSubKey('SYSTEM\CurrentControlSet\Control\Session Manager')
If ($regKey.GetValue('PendingFileRenameOperations') -ne $null)
{
ForEach ($item In $regKey.GetValue('PendingFileRenameOperations'))
# Ignore any VMware drag and drop entries
{ If (($item -ne '') -and ($item -notlike '*VMwareDnD*')) { $rebootRequired = $true; Break } }
}
Try { $regKey.Close() } Catch { }
} Catch { }
Try {
# Check for computer rename updates...
$regKey1 = $reg.OpenSubKey('SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\')
$regKey2 = $reg.OpenSubKey('SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\')
If ($regKey1.GetValue('ComputerName') -ne $regKey2.GetValue('ComputerName')) { $rebootRequired = $true }
Try { $regKey1.Close(); $regKey2.Close() } Catch { }
} Catch { }
$reg.Close()
} Catch { }
Return $rebootRequired
}
Write-Host 'Reboot Pending:' (Check-RebootPending -ComputerName $ComputerName)