Search code examples
powershellrestartrebootchocolateyboxstarter

Should I ever include a reboot command in my BoxStarter script?


Question

Is there ever a reason to include if (Test-PendingReboot) { Invoke-Reboot } in a BoxStarter script when $Boxstarter.RebootOk=$true is declared?

Background

I've recently discovered BoxStarter and noticed that a number of scripts out there include the following code: if (Test-PendingReboot) { Invoke-Reboot }. This includes those scripts with the following options: $Boxstarter.RebootOk=$true and $Boxstarter.AutoLogin=$true; i.e. those which are allowed to reboot and continue as required.

On the BoxStarter site the following statement is made:

Boxstarter intercepts all Chocolatey install commands and checks for pending reboots. If a pending reboot is detected, Boxstarter will reboot the machine and automatically log the user back on and resume the installation.

NB: I understand that Invoke-Reboot may sometimes be required after making changes which would not update the PendingReboot flag; e.g. for certain registry changes to take effect; my question purely relates to the use of this command when wrapped in the if (Test-PendingReboot) statement.

Update: Also asked on Google Groups: https://groups.google.com/forum/#!topic/boxstarter/D0kiRqJyiCY


Solution

  • Personally, I would never do this, no. I rely on Boxstarter looking after this for me, as internally it is doing the same check, so doing it in addition in my script is duplication of effort.

    There are times when, as you mention, you know that a reboot is required for some external reason, so I would directly call Invoke-Reboot, but this would always be surrounded by some guard clause to prevent it happening each time, as I always want my scripts to be repeatable.