Skip to content

Commit 5352f21

Browse files
authored
Fix deployment on PS Gallery, default 'localhost' value, -ExpandResultForSingleComputer switch, formatting (#35)
* formatting (fix whitespace, remove blank lines, expand aliases, brace style K&R/OTBS) * fallback to 'localhost' for $ComputerName with HelpMessage; allow '127.0.0.1' to be used for the local machine * bump version * add switch parameter $ExpandResultForSingleComputer
1 parent a5bff58 commit 5352f21

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

Random Stuff/Test-PendingReboot.ps1

+36-37
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
<#PSScriptInfo
22
3-
.VERSION 1.11
3+
.VERSION 1.13
44
55
.GUID fe3d3698-52fc-40e8-a95c-bbc67a507ed1
66
77
.AUTHOR Adam Bertram
88
99
.COMPANYNAME Adam the Automator, LLC
1010
11-
.COPYRIGHT
11+
.COPYRIGHT
1212
1313
.DESCRIPTION This script tests various registry values to see if the local computer is pending a reboot.
1414
15-
.TAGS
15+
.TAGS
1616
17-
.LICENSEURI
17+
.LICENSEURI
1818
19-
.PROJECTURI
19+
.PROJECTURI
2020
21-
.ICONURI
21+
.ICONURI
2222
23-
.EXTERNALMODULEDEPENDENCIES
23+
.EXTERNALMODULEDEPENDENCIES
2424
25-
.REQUIREDSCRIPTS
25+
.REQUIREDSCRIPTS
2626
27-
.EXTERNALSCRIPTDEPENDENCIES
27+
.EXTERNALSCRIPTDEPENDENCIES
2828
2929
.RELEASENOTES
3030
@@ -34,18 +34,20 @@
3434
Inspiration from: https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
3535
.EXAMPLE
3636
PS> Test-PendingReboot -ComputerName localhost
37-
37+
3838
This example checks various registry values to see if the local computer is pending a reboot.
3939
#>
4040
[CmdletBinding()]
4141
param(
42-
# ComputerName is optional. If not specified, localhost is used.
42+
[Parameter(HelpMessage = 'Optional parameter. If omitted, "localhost" (the local machine) is inferred. If not working locally, the target machine must have PS remoting enabled and winrm running.')]
4343
[ValidateNotNullOrEmpty()]
44-
[string[]]$ComputerName,
45-
44+
[string[]] $ComputerName = 'localhost',
45+
4646
[Parameter()]
4747
[ValidateNotNullOrEmpty()]
48-
[pscredential]$Credential
48+
[pscredential] $Credential,
49+
50+
[switch] $ExpandResultForSingleComputer
4951
)
5052

5153
$ErrorActionPreference = 'Stop'
@@ -65,7 +67,7 @@ $scriptBlock = {
6567
[ValidateNotNullOrEmpty()]
6668
[string]$Key
6769
)
68-
70+
6971
$ErrorActionPreference = 'Stop'
7072

7173
if (Get-Item -Path $Key -ErrorAction Ignore) {
@@ -86,7 +88,7 @@ $scriptBlock = {
8688
[ValidateNotNullOrEmpty()]
8789
[string]$Value
8890
)
89-
91+
9092
$ErrorActionPreference = 'Stop'
9193

9294
if (Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) {
@@ -107,7 +109,7 @@ $scriptBlock = {
107109
[ValidateNotNullOrEmpty()]
108110
[string]$Value
109111
)
110-
112+
111113
$ErrorActionPreference = 'Stop'
112114

113115
if (($regVal = Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) -and $regVal.($Value)) {
@@ -125,13 +127,13 @@ $scriptBlock = {
125127
{ Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting' }
126128
{ Test-RegistryValueNotNull -Key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Value 'PendingFileRenameOperations' }
127129
{ Test-RegistryValueNotNull -Key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Value 'PendingFileRenameOperations2' }
128-
{
130+
{
129131
# Added test to check first if key exists, using "ErrorAction ignore" will incorrectly return $true
130-
'HKLM:\SOFTWARE\Microsoft\Updates' | Where-Object { test-path $_ -PathType Container } | ForEach-Object {
131-
if(Test-Path "$_\UpdateExeVolatile" ){
132-
(Get-ItemProperty -Path $_ -Name 'UpdateExeVolatile' | Select-Object -ExpandProperty UpdateExeVolatile) -ne 0
133-
}else{
134-
$false
132+
'HKLM:\SOFTWARE\Microsoft\Updates' | Where-Object { Test-Path $_ -PathType Container } | ForEach-Object {
133+
if (Test-Path "$_\UpdateExeVolatile" ) {
134+
(Get-ItemProperty -Path $_ -Name 'UpdateExeVolatile' | Select-Object -ExpandProperty UpdateExeVolatile) -ne 0
135+
} else {
136+
$false
135137
}
136138
}
137139
}
@@ -142,12 +144,12 @@ $scriptBlock = {
142144
{
143145
# Added test to check first if keys exists, if not each group will return $Null
144146
# May need to evaluate what it means if one or both of these keys do not exist
145-
( 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' | Where-Object { test-path $_ } | % { (Get-ItemProperty -Path $_ ).ComputerName } ) -ne
146-
( 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' | Where-Object { Test-Path $_ } | % { (Get-ItemProperty -Path $_ ).ComputerName } )
147+
( 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName' | Where-Object { Test-Path $_ } | ForEach-Object { (Get-ItemProperty -Path $_ ).ComputerName } ) -ne
148+
( 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName' | Where-Object { Test-Path $_ } | ForEach-Object { (Get-ItemProperty -Path $_ ).ComputerName } )
147149
}
148150
{
149151
# Added test to check first if key exists
150-
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending' | Where-Object {
152+
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending' | Where-Object {
151153
(Test-Path $_) -and (Get-ChildItem -Path $_) } | ForEach-Object { $true }
152154
}
153155
)
@@ -161,12 +163,6 @@ $scriptBlock = {
161163
}
162164
}
163165

164-
# if ComputerName was not specified, then use localhost
165-
# to ensure that we don't create a Session.
166-
if ($null -eq $ComputerName) {
167-
$ComputerName = "localhost"
168-
}
169-
170166
foreach ($computer in $ComputerName) {
171167
try {
172168
$connParams = @{
@@ -181,19 +177,22 @@ foreach ($computer in $ComputerName) {
181177
IsPendingReboot = $false
182178
}
183179

184-
if ($computer -in ".", "localhost", $env:COMPUTERNAME ) {
180+
if ($computer -iin @('.', 'localhost', '127.0.0.1', $env:COMPUTERNAME) ) {
185181
if (-not ($output.IsPendingReboot = Invoke-Command -ScriptBlock $scriptBlock)) {
186182
$output.IsPendingReboot = $false
187183
}
188-
}
189-
else {
184+
} else {
190185
$psRemotingSession = New-PSSession @connParams
191-
186+
192187
if (-not ($output.IsPendingReboot = Invoke-Command -Session $psRemotingSession -ScriptBlock $scriptBlock)) {
193188
$output.IsPendingReboot = $false
194189
}
195190
}
196-
[pscustomobject]$output
191+
if ((1 -eq $ComputerName.Count) -and $ExpandResultForSingleComputer) {
192+
$output.IsPendingReboot
193+
} else {
194+
[pscustomobject]$output
195+
}
197196
} catch {
198197
Write-Error -Message $_.Exception.Message
199198
} finally {

0 commit comments

Comments
 (0)