I am trying to user Test-Pendingreboot script to find out computers which need reboot, but I don't know how to take the results from Test-pendingreboot if $true the Force Reboot the computer.
Any help will be appreciated
Thank You
I am trying to user Test-Pendingreboot script to find out computers which need reboot, but I don't know how to take the results from Test-pendingreboot if $true the Force Reboot the computer.
Any help will be appreciated
Thank You
I'm assuming you are using this code found at https://ilovepowershell.com/2015/09/10/how-to-check-if-a-server-needs-a-reboot/
#Adapted from https://gist.github.com/altrive/5329377
#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
}
Since it's unclear how you are using this to query a machine it's going to be hard to answer your question accurately. If its checking a local host you can:
$DoINeedAReboot = Test-PendingReboot
if ($DoINeedAReboot) { Restart-Computer }