CheckForPendingReboot.ps1

Applies To: Virtual Machine Manager 2008, Virtual Machine Manager 2008 R2, Virtual Machine Manager 2008 R2 SP1

There are times when you might want to check a computer for a pending reboot before proceeding with an operation, such as adding a computer as a host to System Center Virtual Machine Manager (VMM). You can verify whether the computer is pending a reboot by looking for the RebootPending registry key. The script in this topic looks for that key and, if the key is present, will restart the computer upon confirmation.

Note

The Restart-Computer cmdlet is supported only in Windows PowerShell 2.0. If you are running the script from a computer with Windows PowerShell 1.0 installed, comment out this line in the script to prevent the script from throwing an error.

The script imports a list of computers from a comma-separated value (CSV) file. Therefore, prior to running the script, you will need to create a CSV file. You can create a CSV file manually by using a text editor or by exporting data to a CSV file from a program such as Microsoft Excel. The script assumes that you have used "ComputerName" as the column header and that you have named the CSV file "computers.csv". Your CSV file should look similar to the following:

ComputerName
Server01
Server02
Server03

Prior to running the script, ensure that you update the $CSVPath variable with the actual path to your CSV file.

Disclaimer

# Filename:      CheckForPendingReboot.ps1
# Description:   Imports a list of computers from a CSV file and then checks each of the
#                computers for the RebootPending registry key. If the key is present, 
#                the script restarts the computer upon confirmation from the user.

$CSVPath = "C:\myscripts\computers.csv"
$Computers = Import-CSV -Path $CSVPath 

ForEach ($Machine in $Computers)
{
   $baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $Machine.computername)
   $key = $baseKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
   $subkeys = $key.GetSubKeyNames()
   $key.Close()
   $baseKey.Close()

   If ($subkeys | Where {$_ -eq "RebootPending"}) 
   {
      Write-Host "There is a pending reboot for" $Machine.computername
      Restart-Computer -ComputerName $Machine.computername -confirm
   }
   Else 
   {
      Write-Host "No reboot is pending for" $Machine.computername
   }
}