Skip to content

Commit

Permalink
Replace problematic PendingReboot module with custom function
Browse files Browse the repository at this point in the history
  • Loading branch information
AgenttiX committed Nov 2, 2024
1 parent 6d4d585 commit 36e2028
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 43 deletions.
6 changes: 4 additions & 2 deletions Maintenance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ if (Test-CommandExists "docker") {
# This should be the last step in the script so that its updates are not installed during other updates.
if ($Reboot -or $Shutdown) {
Show-Output -ForegroundColor Cyan "Driver updates will not be started, as automatic reboot or shutdown is enabled."
} elseif ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
# } elseif ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
} elseif (Test-RebootPending) {
Show-Output -ForegroundColor Cyan "Driver updates will not be started, as the computer is pending a reboot."
} else {
# Lenovo Vantage (non-blocking)
Expand Down Expand Up @@ -620,7 +621,8 @@ if ($Reboot) {
Show-Output -ForegroundColor Cyan "The computer will be shut down in 10 seconds."
shutdown /s /t 10 /c "Mika's maintenance script is ready. Shutting down."
} else {
if ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
# if ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
if (Test-RebootPending) {
Show-Output -ForegroundColor Cyan "The computer is pending a reboot. Please reboot the computer, once all the updater windows that are open say that they are ready."
}
Show-Output -ForegroundColor Green "You can now close this window."
Expand Down
84 changes: 43 additions & 41 deletions Utils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -695,54 +695,56 @@ Function Test-CommandExists {
}

function Test-PendingRebootAndExit {
if (Test-CommandExists "Install-Module") {
Show-Output -ForegroundColor Cyan "Ensuring that the PowerShell reboot checker module is installed. You may now be asked whether to install the NuGet package provider. Please select yes."
Install-Module -Name PendingReboot -Force
}
if ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
# The PendingReboot module gives false positives about the need to reboot.
# if (Test-CommandExists "Install-Module") {
# Show-Output -ForegroundColor Cyan "Ensuring that the PowerShell reboot checker module is installed. You may now be asked whether to install the NuGet package provider. Please select yes."
# Install-Module -Name PendingReboot -Force
# }
# if ((Test-CommandExists "Test-PendingReboot") -and (Test-PendingReboot -SkipConfigurationManagerClientCheck -SkipPendingFileRenameOperationsCheck).IsRebootPending) {
if (Test-RebootPending) {
Show-Output -ForegroundColor Cyan "A reboot is already pending. Please close this window, reboot the computer and then run this script again."
if (! (Get-YesNo "If you are sure you want to continue regardless, please write `"y`" and press enter.")) {
Exit 0
}
}
}

# function Test-RebootPending {
# <#
# .SYNOPSIS
# Test whether the computer has a reboot pending.
# .LINK
# https://4sysops.com/archives/use-powershell-to-test-if-a-windows-server-is-pending-a-reboot/
# #>
# [OutputType([bool])]
# $pendingRebootTests = @(
# @{
# Name = "RebootPending"
# Test = { Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Name "RebootPending" -ErrorAction Ignore }
# TestType = "ValueExists"
# }
# @{
# Name = "RebootRequired"
# Test = { Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction Ignore }
# TestType = "ValueExists"
# }
# @{
# Name = "PendingFileRenameOperations"
# Test = { Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name "PendingFileRenameOperations" -ErrorAction Ignore }
# TestType = "NonNullValue"
# }
# )
# foreach ($test in $pendingRebootTests) {
# $result = Invoke-Command -ScriptBlock $test.Test
# if ($test.TestType -eq "ValueExists" -and $result) {
# return $true
# } elseif ($test.TestType -eq "NonNullValue" -and $result -and $result.($test.Name)) {
# return $true
# } else {
# return $false
# }
# }
# }
function Test-RebootPending {
<#
.SYNOPSIS
Test whether the computer has a reboot pending. The name is chosen not to conflict with the PendingReboot PowerShell module.
.LINK
https://4sysops.com/archives/use-powershell-to-test-if-a-windows-server-is-pending-a-reboot/
#>
[OutputType([bool])]
$pendingRebootTests = @(
@{
Name = "RebootPending"
Test = { Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing" -Name "RebootPending" -ErrorAction Ignore }
TestType = "ValueExists"
}
@{
Name = "RebootRequired"
Test = { Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction Ignore }
TestType = "ValueExists"
}
@{
Name = "PendingFileRenameOperations"
Test = { Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name "PendingFileRenameOperations" -ErrorAction Ignore }
TestType = "NonNullValue"
}
)
foreach ($test in $pendingRebootTests) {
$result = Invoke-Command -ScriptBlock $test.Test
if ($test.TestType -eq "ValueExists" -and $result) {
return $true
} elseif ($test.TestType -eq "NonNullValue" -and $result -and $result.($test.Name)) {
return $true
} else {
return $false
}
}
}

function Update-Repo {
<#
Expand Down

0 comments on commit 36e2028

Please sign in to comment.