0Day Forums
How can I check for a pending reboot? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: PowerShell & .ps1 (https://zeroday.vip/Forum-PowerShell-ps1)
+--- Thread: How can I check for a pending reboot? (/Thread-How-can-I-check-for-a-pending-reboot)



How can I check for a pending reboot? - pinwheel944 - 07-21-2023

I am trying to get to know where reboot is required or not for a Windows machine. However, my script is throwing and error.

powershell "$key = Get-Item "HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue"

Error :
Get-Item : A positional parameter cannot be found that accepts argument
'Update\RebootRequired'.
At line:1 char:8
+ $key = Get-Item
HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Aut ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
+ CategoryInfo : InvalidArgument: (:) [Get-Item], ParameterBindin
gException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell
.Commands.GetItemCommand

I am running this command in "command prompt". Not sure what it means !


RE: How can I check for a pending reboot? - calycle972986 - 07-21-2023


Your syntax wasn't correct, if you want to run the PowerShell command from cmd, it has to look like this:

powershell.exe "Get-Item 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'"

But like Mathis mentioned, this key only exists if a reboot is pending.


RE: How can I check for a pending reboot? - plagues278518 - 07-21-2023

Pending reboot can be caused by variety of reasons, not just the ones that are detailed in other answers. Try [PendingReboot][1] module, which incorporates various tests into a single cmdlet:

# Install
Install-Module -Name PendingReboot

# Run
Test-PendingReboot -Detailed


[1]:

[To see links please register here]




RE: How can I check for a pending reboot? - palnymmhpjr - 07-21-2023

One thing I found that was causing this (and no end of headaches for me) was every time I tried to run the SCCM 1906 update it failed due to a pending reboot. Using this script in my investigations, I noticed it was ComponentBasedServicing that seemed to be holding up the works, which was the Optional Components were automatically installing. A little bit more digging lead me to a scheduled task called LanguageComponentsInstaller. I disabled this and I'm keeping an eye on it but it seems to have fixed this problem.

Thanks for the script. It's saved me a lot of stress trying to crack this egg :)


RE: How can I check for a pending reboot? - azariahn - 07-21-2023

You need to check 2 paths, one key and you need to query the configuration manager via `WMI` in order to check all possible locations.

```
#Adapted from
#Based on <http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542>
function Test-PendingReboot {
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true }
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true }
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if (($status -ne $null) -and $status.RebootPending) {
return $true
}
}
catch { }

return $false
}

Test-PendingReboot
```


RE: How can I check for a pending reboot? - Sirremote725 - 07-21-2023

(I would have preferred to add this as a comment on the accepted answer, but the code would not have fit.)

I think the following function can eliminate some unnecessary reboots. The `PendingFileRenameOperations` registry key supports not only renames, but also deletes (expressed essentially as "rename to null")<sup>*</sup>. The assumption I am making is that deletes represent pending cleanup operations that will not affect functionality in the meantime.

```
<#
.SYNOPSIS
Returns true if any true renames-- deletes are ignored-- are present in the
PendingFileRenameOperations registry key.
#>
function Test-PendingFileRename {
[OutputType('bool')]
[CmdletBinding()]
param()
$operations = (Get-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\').GetValue('PendingFileRenameOperations')
if ($null -eq $operations) {
$false
} else {
$trueOperationsCount = $operations.Length / 2
$trueRenames = [System.Collections.Generic.Dictionary[string, string]]::new($trueOperationsCount)
for ($i = 0; $i -ne $trueOperationsCount; $i++) {
$operationSource = $operations[$i * 2]
$operationDestination = $operations[$i * 2 + 1]
if ($operationDestination.Length -eq 0) {
Write-Verbose "Ignoring pending file delete '$operationSource'"
} else {
Write-Host "Found a true pending file rename (as opposed to delete). Source '$operationSource'; Dest '$operationDestination'"
$trueRenames[$operationSource] = $operationDestination
}
}
$trueRenames.Count -gt 0
}
}
```
One would implement this in the accepted answer's script by inserting the above function at the top and then replacing the line
```
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true }
```
with
```
if (Test-PendingFileRename) { return $true }
```

<sup>*</sup> refs:
* [Interpreting the PendingFileRenameOperations Registry Key, J Jewitt](

[To see links please register here]

)
* [MoveFile Utility documentation, Microsoft](

[To see links please register here]

)