In this video I demonstrate how you can get pending reboot information on Windows using remoting techniques.
*list of servers needing reboot
*reboot pending
*how to detect pending reboots on windows
*powershell
*learn powershell
*automation
*learn automation
*windows
*windows powershell
*automatic deployment
*automatic installations
Code:
Function Get-PendingReboot {
Param(
[Parameter(Mandatory=$True, ValueFromPipeline=$True)] $Machine,
[Parameter(Mandatory=$True)] $creds
)
Try {
Invoke-Command -ComputerName $Machine -Credential $creds -ScriptBlock {
$RebootPending = $False
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("LocalMachine", $Machine)
$key = $baseKey.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\")
$subkeys = $key.GetSubKeyNames()
$key.Close()
If ($subkeys | Where {$_ -eq "RebootPending"}){
$RebootPending = $true
}
$RegWUAU = $baseKey.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
$RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
If ($RegWUAURebootReq | Where {$_ -eq "RebootRequired"})
{
$RebootPending = $true
}
$baseKey.Close()
if(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction SilentlyContinue){
$RebootPending = $true
}
$props = @{
rebootPending = $RebootPending
}
New-Object psobject -Property $props
}
}
Catch{
Write-Error "ERROR, trying to get regkey on machine : $Machine, Error returned = $($_.Exception.Message)"
}
}
$creds = Get-Credential
$statusall = [System.Collections.ArrayList]@()
foreach($server in get-content servers.txt){
$statusall.Add((Get-PendingReboot -Machine $server -creds $creds))
}
$statusall | select rebootpending, PSComputername
$statusall.where({$PSItem.rebootPending -eq $true}) | select -ExpandProperty PSComputername | out-file reboots.txt
Watch video PowerShell - Pending reboots online, duration hours minute second in high quality that is uploaded to the channel Mr Automation 26 February 2022. Share the link to the video on social media so that your subscribers and friends will also watch this video. This video clip has been viewed 1,166 times and liked it 31 visitors.