Skip to content

Instantly share code, notes, and snippets.

@lennybacon
Created January 16, 2020 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lennybacon/014665bd61edfc927fb033d69742ace6 to your computer and use it in GitHub Desktop.
Save lennybacon/014665bd61edfc927fb033d69742ace6 to your computer and use it in GitHub Desktop.
Detect a pending reboot and restart computer
$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