PowerShell - Pending reboots

Опубликовано: 26 Февраль 2022
на канале: Mr Automation
1,166
31

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


Смотрите видео PowerShell - Pending reboots онлайн, длительностью часов минут секунд в хорошем качестве, которое загружено на канал Mr Automation 26 Февраль 2022. Делитесь ссылкой на видео в социальных сетях, чтобы ваши подписчики и друзья так же посмотрели это видео. Данный видеоклип посмотрели 1,166 раз и оно понравилось 31 посетителям.