0

This question was asked a few times before but I could not find a solution to my scenario in any of them.

Basically I need the script to continue after it reboots if needed. It will check a few registry keys and will determine if the machine needs to be rebooted.

I tried using 'workflow' in various ways but could not get it to work successfully.

Here is the rough description of my code:

function (check-if-computer-needs-a-reboot){
    if(this and that)

    try{

    return $true
    }
    catch{}

    return $false
    }

if(check-if-computer-needs-a-reboot = $true){

Write-Host "The machine is rebooting..."
Restart-Computer -Wait -Force
Write-Host "The machine was successfully rebooted"

}

    else 

    {
    Write-Host "No pending reboot" 
    }

Hoping the wizards of Stack-overflow can help.

Any help will be greatly appreciated!!!

Eran Mor
  • 85
  • 1
  • 13
  • Duplicate: https://stackoverflow.com/questions/15482174/how-can-a-required-reboot-be-detected-for-windows-7 – Maximilian Burszley Feb 11 '19 at 20:14
  • 1
    @TheIncorrigible1 What if there is a better answer then the duplicate posted? I feel like that answer is incomplete as there are multiable places you need to check and that only suggests one – ArcSet Feb 11 '19 at 20:16
  • 1
    I have only C# code for taht, but literally you need to check 4 aspects: 1) `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName` and `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName` -- if not matched, then pending. 2) if `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending` exists, then pending. 3) if any operations are listed under `HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations` -- pending. – Max Feb 11 '19 at 21:14
  • 1
    and 4) if `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Updates\UpdateExeVolatile` exists, then pending. Finally, if you have SCCM client installed 5) `\ROOT\ccm\ClientSDK:CCM_ClientUtilities.DetermineIfRebootPending` WMI method returns true in the `RebootPending` param, then pending. – Max Feb 11 '19 at 21:15
  • 1
    Thank you guys for responding. I did find all the registry keys I needed in order to check if the machine needs to be rebooted alright but what I could not find is how to get this to actually use workflow containing a function and other commands. @TheIncorrigible1, I don's see this as duplicate at all since it is not talking about continuing the script after a machine was rebooted. – Eran Mor Feb 11 '19 at 21:54

1 Answers1

0

To continue doing something after a reboot, you need to add a value to the Runonce registry key to specify what to do after the reboot.

Break break your script into two parts (Preboot and Postboot). Put the following at the end of Preboot.ps1:

if(check-if-computer-needs-a-reboot){
  REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Runonce" /V "Postboot" /d "\"%~dpPostboot.ps1\" MyParameters" /t REG_SZ /f
  Write-Host "The machine is rebooting..."
  Restart-Computer -Wait -Force
}
# Fall through if reboot was not needed, so continue with part 2
Write-Host "No pending reboot... continuing"
"%~dpPostboot.ps1"

Notes:

  1. I copied this from a .bat file. In a .bat file "%~dp0" means "the drive and path that the current script is running from". The syntax in Powershell might be a little different.

  2. While Powershell can run other programs such as REG.EXE, Powershell has its own built-in HKLM: PSdrive. That should be more efficient than using REG.EXE. I leave it to you to do the conversion.

  3. In your code, you test if a Boolean value "equals" $True. This comparison is never necessary unless the value isn't really Boolean and could be something other than True or False. Just test the Boolean value itself as I've shown above.