diff --git a/Maintenance.ps1 b/Maintenance.ps1 index 816cc08..99fc867 100644 --- a/Maintenance.ps1 +++ b/Maintenance.ps1 @@ -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) @@ -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." diff --git a/Utils.ps1 b/Utils.ps1 index 6849043..5299689 100644 --- a/Utils.ps1 +++ b/Utils.ps1 @@ -695,11 +695,13 @@ 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 @@ -707,42 +709,42 @@ function Test-PendingRebootAndExit { } } -# 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 { <#