-1

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

mklement0
  • 382,024
  • 64
  • 607
  • 775
R0b
  • 11
  • 1
  • 3

1 Answers1

2

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 }
CM42
  • 80
  • 7
  • Thank youn for the reply yes you are right abput the website I got it from there but the problem is some of the computers dont have that installed. So I tried to install the script on all the computers but that fails to. If I use the abput script it creates a function that O can ise but I need the Test-pendingreboot to be installed on the local or remote computer – R0b Jan 22 '20 at 03:52
  • The function you listed is not really written for remote use. If you want to query remote machines you will need to use PowerShell remoting which you can find info on here https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers but you might want to just look at this persons post who has already done what you are asking https://adamtheautomator.com/pending-reboot-registry-windows/ – CM42 Jan 22 '20 at 04:15
  • Thank You I will try the web link tomorrow. – R0b Jan 22 '20 at 04:17