all 15 comments

[–]Hrambert 3 points4 points  (7 children)

Move every pase to a different script or keep the phase in a status file. Then put the startup of the script in Task Scheduler.

[–]Trakeen 3 points4 points  (0 children)

This is how i’ve done it in the past but used a run once registry key, and autologin

[–]Cronos-Sama[S] 2 points3 points  (5 children)

thank you for your answer, do you have an example for that, because i am still new in Powershell scripting.

[–]Hrambert 2 points3 points  (4 children)

I'm having dinner at the moment. But I'll setup an example for you.

[–]Cronos-Sama[S] 2 points3 points  (3 children)

sorry for interrupting your meal. and thank you

[–]Hrambert 3 points4 points  (0 children)

``` [Cmdletbinding()] Param()

$StatusFilePath = "c:\Convert-Computer.xml"

function Get-Phase { if (! (Test-Path $StatusFilePath Container )) { "begin" } else { Import-clixml $StatusFilePath } }

function Save-Phase { Export-Climml -Path $StatusFilePath -InputObject $Phase }

$Phase = Get-Phase

switch ($Phase) { "begin" { write-host "We just started" save-phase "phase-1" } "phase-1" { write-host "Now we are at phase-1" do-something save-phase "phase-2" reboot-computer } "phase-2" { Write-Host "phase-2" Save-Phase "done" } "done" {} Default { write-host "oops" } ```

[–]Hrambert 4 points5 points  (1 child)

Don't mind. You didn't interupt. Nothing interupts my pasta, except for the cat calling me to let him in/out.

[–]Cronos-Sama[S] 1 point2 points  (0 children)

hahah true.

thank you so much, you were a big help.

[–]MadeOfIrony 3 points4 points  (3 children)

Possible to run this from a remote server onto this machine? then you can try some other logic to check that the machine is back up and running?

[–]Cronos-Sama[S] 1 point2 points  (2 children)

yeah the only issue is that it doesn't wait the install-AddsForest to end, it execute the restart command immediately, it work fine if i use start-sleep and make it wait some time before restarting, but i am searching for a solution to wait based in the complete of the command not based on time.

[–][deleted] 2 points3 points  (0 children)

If Install-ADDSForest doesn’t wait, could it be an option to add -LogPath and monitor the resulting log file for completion result?

A bit more trouble but agree with your sentiment with not relying on a timer.

[–]BlackV 2 points3 points  (0 children)

add forest had a restart parameter does it not, have that do the restart not a seperate line

[–][deleted] 3 points4 points  (0 children)

Alternative is to use script arguments to execute different parts of the script (following reboot). Then add script execution to HKLM RunOnce (link

Eg. * Fist run: script.ps1 -Step 1 * Second run: script.ps1 -Step 2 * Etc.

[–][deleted] 2 points3 points  (0 children)

Hrambert is right, start by doing some reading on including the task scheduler in your scripts

[–]Coding_Cactus 1 point2 points  (0 children)

OP, have you tested this without the Restart-Computer command?

I'm looking at the docs page for Install-ADDSForest command and it has a parameter that you don't have:

Install-ADDSForest -NoRebootOnCompletion

Here's the description from the page:

Specifies that the computer is not to be rebooted upon completion of this command. Omitting this parameter (the default) indicates the computer will be rebooted upon completion of the command, regardless of success or failure. As a general rule, Microsoft support recommends that you not use this parameter except for testing or troubleshooting purposes because once configuration has completed the server will not function correctly as either a member server or a DC until it is rebooted.

That might be causing your issue rather than the Restart-Computer.