Created
January 16, 2020 10:38
-
-
Save lennybacon/014665bd61edfc927fb033d69742ace6 to your computer and use it in GitHub Desktop.
Detect a pending reboot and restart computer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$registryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"); | |
if ($null -ne $registryKey){ | |
if ($registryKey.GetValueNames().Length -ne 0) { | |
Write-Host "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired indicates a pending reboot."; | |
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 99 –Message "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired indicates a pending reboot."; | |
Restart-Computer -Force | |
} | |
} | |
$registryKey = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Updates"); | |
if ($null -ne $registryKey){ | |
$updateExeVolatile = $registryKey.GetValue("UpdateExeVolatile"); | |
if ($null -ne $updateExeVolatile) { | |
Write-Host "HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile indicates a pending reboot."; | |
Write-EventLog –LogName Application –Source $EventLogSourceName –EntryType Information –EventID 99 –Message "HKLM\SOFTWARE\Microsoft\Updates\UpdateExeVolatile indicates a pending reboot."; | |
Restart-Computer -Force | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment