1

I'm using an autounattend.xml file to perform an unattended installation of Windows Server 2022.

In the specialize phase, I have a few Powershell scripts I want to run (defined in RunSynchronousCommand elements).

I need to make sure that if either one of these scripts happens to fail, the whole installation process is interrupted right away. I would have expected an uncaught exception would do just that but much to my surprise it doesn't. It just goes through the next step.

How can I make sure that the Windows installation gets interrupted whenever a custom script of mine fails? Am I supposed to exit with some sort of special code?

0

1 Answer 1

1

Per WillReboot if you use OnRequest and the return code is not 0-2 then "The command failed. An error must be returned and installation terminated.".

Apparently this works with both "specialize" and "auditUser". If defined and configured properly, this should terminate the installation as it says this option does.

If the value of WillReboot is OnRequest, the synchronous command must return one of the following codes.

  • Return code: 0
  • Description: The command was successful. No reboot is required.

  • Return code: 1
  • Description: The command was successful. An immediate reboot is required. Then, the next command can be started.

  • Return code: 2
  • Description: The command is still in process. An immediate reboot is required. Then, the same command must be restarted. This code can be returned multiple times.

  • Return code: <anything else>
  • Description: The command failed. An error must be returned and installation terminated.

Source

Other useful detail

I would also add that a command within Path element that calls powershell.exe scriptfile.ps1 may end up abstracting exit codes from script file. In my case, even though the script ends with Exit 10, the ending %errorlevel% value was still 1, as in "Powershell did not exit with a 0". To work around this, I modified the command to 'powershell -command "& ./scriptfile.ps1;exit $LASTEXITCODE;"'. – Crono

Exceptions

Furthermore, for unhandled exception capturing (or triggering one), I find the Try{}Catch{} useful most of the time. For example $e=try{<logic>}Catch{$False}; If(!$e){$FreakOut=$True} or some variation as such to help trigger one if it isn't otherwise by the executing process.

If all else fails and you need to capture everything you can't see the output on at runtime, incorporate start-transcript and inspect its log after the execution run being troubleshot.

Supporting Resources

2
  • I would also add that a command within Path element that calls powershell.exe scriptfile.ps1 may end up abstracting exit codes from script file. In my case, even though the script ends with Exit 10, the ending %errorlevel% value was still 1, as in "Powershell did not exit with a 0". To work around this, I modified the command to powershell -command "& ./scriptfile.ps1;exit $LASTEXITCODE;".
    – Crono
    Commented Jan 18, 2023 at 18:43
  • 1
    Thank you @Crono, I concatenated this useful tip into a new section in the answer to ensure preservation outside of the comment area just in case. Commented Jan 18, 2023 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .