-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest-PendingReboot.ps1
146 lines (119 loc) · 4.88 KB
/
Test-PendingReboot.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<#PSScriptInfo
.VERSION 1.4
.GUID fe3d3698-52fc-40e8-a95c-bbc67a507ed1
.AUTHOR Adam Bertram
.COMPANYNAME Adam the Automator, LLC
.COPYRIGHT
.DESCRIPTION This function tests various registry values to see if the local computer is pending a reboot.
.TAGS
.LICENSEURI
.PROJECTURI
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
.SYNOPSIS
This function tests various registry values to see if the local computer is pending a reboot
.NOTES
Inspiration from: https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
Credential handling inspired by http://duffney.io/AddCredentialsToPowerShellFunctions
Edited by JBLewis (@AspenForester)
.EXAMPLE
PS> Test-PendingReboot -computername itfsrpw003
IsPendingReboot ComputerName
--------------- ------------
True itfsrpw003
This example checks various registry values to see if the local computer is pending a reboot.
.EXAMPLE
PS> Test-PendingReboot -computername itfsrpw003 -Quiet
True
The Quiet switch limits the output to a simple True or False
#>
Function Test-PendingReboot
{
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string[]]
$ComputerName,
[Parameter()]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
# Reduces output to simple boolean
[Parameter()]
[Switch]
$Quiet
)
process
{
$ErrorActionPreference = 'Stop'
try
{
foreach ($computer in $ComputerName)
{
$connParams = @{
'ComputerName' = $computer
}
if ($PSBoundParameters.ContainsKey('Credential'))
{
$connParams.Credential = $Credential
}
$output = @{
ComputerName = $computer
IsPendingReboot = $false
}
$cimSession = New-CimSession @connParams
$OperatingSystem = Get-CimInstance -CimSession $cimSession -ClassName Win32_OperatingSystem -Property BuildNumber, CSName
$Session = New-PSSession @connParams
$ICSplat = @{
Session = $Session
}
# If Vista/2008 & Above query the CBS Reg Key
If ($OperatingSystem.BuildNumber -ge 6001 -and (Invoke-Command @ICSplat -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' -Name 'RebootPending' -ErrorAction SilentlyContinue }))
{
Write-Verbose -Message 'Reboot pending detected in the Component Based Servicing registry key'
$output.IsPendingReboot = $true
}
elseif (Invoke-Command @ICSplat -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' -Name 'RebootRequired' -ErrorAction SilentlyContinue })
{
Write-Verbose -Message 'WUAU has a reboot pending'
$output.IsPendingReboot = $true
}
elseif (Invoke-Command @ICSplat -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction SilentlyContinue } -OutVariable 'PendingReboot')
{
if ($PendingReboot.PendingFileRenameOperations)
{
Write-Verbose -Message 'Reboot pending in the PendingFileRenameOperations registry value'
$output.IsPendingReboot = $true
}
}
elseif (Invoke-Command @ICSplat -ScriptBlock { (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' -ErrorAction SilentlyContinue).Computername -ne (Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\computername' -ErrorAction SilentlyContinue).Computername})
{
Write-Verbose -Message 'Computer name change is pending'
$output.IsPendingReboot = $true
}
If ($PSBoundParameters.ContainsKey('Quiet'))
{
Write-Output $output.IsPendingReboot
}
else
{
[pscustomobject]$output
}
}
}
catch
{
Write-Error -Message $_.Exception.Message
}
}
end
{
$Session | Remove-PSSession
$ErrorActionPreference = 'Continue'
}
}