After the security patch, we need to validate if any of our
critical server is pending for reboot and take necessary actions.PowerShell script and use it to identify servers that
are pending for reboot.This will avoid the scenarios of unexpected server reboot.
# Filename: CheckForPendingReboot.ps1
# Description: Imports a list of computers from a text 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.
# Assumes the existence of a file called 'Servers.txt' in the same directory, populated with
# the NETBIOS names of a list of computers, each on a new line.
# Description: Imports a list of computers from a text 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.
# Assumes the existence of a file called 'Servers.txt' in the same directory, populated with
# the NETBIOS names of a list of computers, each on a new line.
$computernames = gc Servers.txt
foreach ($computername in $computernames)
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$computername)
$key = $baseKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
$subkeys = $key.GetSubKeyNames()
$key.Close()
$baseKey.Close()
foreach ($computername in $computernames)
{
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine",$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" $computername
Restart-Computer -ComputerName $computername -confirm
}
Else
{
Write-Host "No reboot is pending for" $computername
}
}
{
Write-Host "There is a pending reboot for" $computername
Restart-Computer -ComputerName $computername -confirm
}
Else
{
Write-Host "No reboot is pending for" $computername
}
}
Very Useful
ReplyDeletethanks
Exception calling "OpenRemoteBaseKey" with "2" argument(s): "The network path was not found."
ReplyDeleteFor one of my server, it is pinging, server registry service is started.i can take this server remotely. can connect regedit through remote computer mmc.
Thank you it worked.
ReplyDeleteLooking for Hardware Support
what is your mail id roli.
ReplyDeleteHi Roli,
ReplyDeleteI have a query, lets see in our enviornment we have 100 machine. we want to initiate the reboot all the server. which name i kept in notepad.
however if some of the server doesnt reboot successfully. i want a list which all server doesnt reboot.
which all got rebooted.