powershellexit-codemdt

Powershell returning exit code 0 or 1 in MDT Task Sequence


I think I'm breaking my head over something very simple. I've been working on it for two days and unfortunately I can't figure it out.

The problem is that I'm using a Powershell script while deploying a Windows system with MDT. The script is called last in the task sequence. In the script I ask for a reboot and retry task because the application (Dell update tool) needs a reboot.

This part works, as in MDT TS it understands that it needs to reboot and also needs to restart the task. The machine reboots and then reruns the same Task Sequence but the the script is never fired while the SMSTS.log does say that it was started successfully and exited with code 0.

I then noticed that the exit code is always 0 while I change this in my script. Then I found out that it goes wrong with the return code of my powershell script and tried the following:

The test script:

$MyExitCode = 3010
Write-Host ('MyExitcode: [' + $MyExitCode + ']')
Write-Host ('LastExitCode: [' + $LastExitCode + ']')
Write-Host ''
Write-Host 'Setting MyExitCode to LastExitCode'
$LastExitCode = $MyExitCode
Write-Host 'New Result:'
Write-Host ('MyExitcode: [' + $MyExitCode + ']')
Write-Host ('LastExitCode: [' + $LastExitCode + ']')
Read-Host 'Debug Break' 
Write-Output $_
Exit $ExitCode

Open CMD as admin and then run the following command

CMD> powershell.exe -ExecutionPolicy bypass -File "Z:\Applications\Test\TestRerunTask.ps1"
CMD> echo $LastExitCode
CMD> 0

Replacing the parameter -File with -Command changed the exit code to 1 but still in the SMSTS.log in says it exited with code 0

Snippet SMSTS.log the Task Sequence is called Test

!--------------------------------------------------------------------------------------------!  TSManager   13-2-2020 11:18:41  6200 (0x1838)
Expand a string: WinPEandFullOS                                                                 TSManager   13-2-2020 11:18:41  6200 (0x1838)
Executing command line: cscript.exe "%SCRIPTROOT%\ZTIApplications.wsf"                          TSManager   13-2-2020 11:18:41  6200 (0x1838)
Process completed with exit code 0                                                              TSManager   13-2-2020 11:19:02  6200 (0x1838)
!--------------------------------------------------------------------------------------------!  TSManager   13-2-2020 11:19:02  6200 (0x1838)
Successfully completed the action (Test) with the exit win32 code 0                             TSManager   13-2-2020 11:19:02  6200 (0x1838)
Executing in non SMS standalone mode. Ignoring send a task execution status message request     TSManager   13-2-2020 11:19:02  6200 (0x1838)
Set a global environment variable _SMSTSLastActionRetCode=0                                     TSManager   13-2-2020 11:19:02  6200 (0x1838)
Set a global environment variable _SMSTSLastActionName=Test                                     TSManager   13-2-2020 11:19:02  6200 (0x1838)
Set a global environment variable _SMSTSLastActionSucceeded=true                                TSManager   13-2-2020 11:19:02  6200 (0x1838)
Clear local default environment                                                                 TSManager   13-2-2020 11:19:02  6200 (0x1838)
The action (Test) requested a retry                                                             TSManager   13-2-2020 11:19:02  6200 (0x1838)
Created volatile registry entry for pending reboot initiated by this task sequence              TSManager   13-2-2020 11:19:02  6200 (0x1838)
The action (Test) initiated a reboot request                                                    TSManager   13-2-2020 11:19:02  6200 (0x1838)
Executing in non SMS standalone mode. Ignoring send a task execution status message request     TSManager   13-2-2020 11:19:02  6200 (0x1838)
****************************************************************************                    TSManager   13-2-2020 11:19:02  6200 (0x1838)
Execution engine result code: Reboot (2)                                                        TSManager   13-2-2020 11:19:02  6200 (0x1838)
Process completed with exit code 2147945410                                                     TSMBootstrap    13-2-2020 11:19:02  6168 (0x1818)
Exiting with return code 0x80070BC2                                                             TSMBootstrap    13-2-2020 11:19:02  6168 (0x1818)
----------------------
... More reboot stuff ...
----------------------
Expand a string: WinPEandFullOS                                                                 TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Executing command line: cscript.exe "%SCRIPTROOT%\ZTIApplications.wsf"                          TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Process completed with exit code 0                                                              TSManager   13-2-2020 11:19:34  8652 (0x21CC)
!--------------------------------------------------------------------------------------------!  TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Successfully completed the action (Test) with the exit win32 code 0                             TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Executing in non SMS standalone mode. Ignoring send a task execution status message request     TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Set a global environment variable _SMSTSLastActionRetCode=0                                     TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Set a global environment variable _SMSTSLastActionName=Test                                     TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Set a global environment variable _SMSTSLastActionSucceeded=true                                TSManager   13-2-2020 11:19:34  8652 (0x21CC)
Clear local default environment                                                                 TSManager   13-2-2020 11:19:34  8652 (0x21CC)

Solution

  • After some more debugging and googling I found the answer I was looking for. First I found this and then this topic. These topics pointed me in te right direction. It turns out that i'm running a Powershell script as an Application in MDT Task Sequence. I find it easy to add scripts as applications because I can then easily assign them to a Task Sequence BUT this is where the magic of the return code is lost. The ZTIapplications script runs the powershell script as powershell.exe -ExecutionPolicy bypass -Command "& { & '%deployroot%\Applications\Test\TestRerunTask.ps1'; Exit $LastExitCode }" this captures the return code but the return code isn't passed back to the Task Sequence SMSTS so it assumes that the "Application" was installed successfully. Calling the script from the MDT Run Powershell Script everything works as expected.

    I'm still looking for a way to still use Powershell script as an Application in MDT keeping the return code working all the way up but for the meantime I have a suitable workaround