Powershell 在 MDT 任务序列中返回退出代码 0 或 1

Powershell returning exit code 0 or 1 in MDT Task Sequence

我想我正在为一些非常简单的事情而伤脑筋。我已经研究了两天,不幸的是我无法弄清楚。

问题是我在使用 MDT 部署 Windows 系统时使用了 Powershell 脚本。该脚本在任务序列中最后调用。在脚本中,我要求重新启动并重试任务,因为应用程序(戴尔更新工具)需要重新启动。

这部分有效,因为在 MDT TS 中它知道它需要重新启动并且还需要重新启动任务。机器重新启动,然后重新运行s 相同的任务序列,但脚本永远不会被触发,而 SMSTS.log 确实说它已成功启动并以代码 0 退出。

然后我注意到在我的脚本中更改退出代码时退出代码始终为 0。然后我发现我的 powershell 脚本的 return 代码出错了,并尝试了以下操作:

测试脚本:

$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

以管理员身份打开CMD,然后运行执行以下命令

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

-Command 替换参数 -File 将退出代码更改为 1 但仍在 SMSTS.log 中表示它以代码 0

退出

代码段 SMSTS.log 任务序列被调用 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)

经过更多的调试和谷歌搜索,我找到了我正在寻找的答案。首先我找到了 this and then this 话题。这些主题为我指明了正确的方向。事实证明,我是 运行 一个 Powershell 脚本,作为 MDT 任务序列中的 Application。我发现将脚本添加为应用程序很容易,因为我可以轻松地将它们分配给任务序列 BUT 但是 return code 的魔力就在这里消失了。 ZTIapplications 脚本以 powershell.exe -ExecutionPolicy bypass -Command "& { & '%deployroot%\Applications\Test\TestRerunTask.ps1'; Exit $LastExitCode }" 运行 powershell 脚本,这会捕获 return 代码,但 return 代码不会传回任务序列 SMSTS,因此它假定 "Application" 已成功安装。从 MDT 调用脚本 Run Powershell Script 一切正常。

我仍在寻找一种方法来继续将 Powershell 脚本用作 MDT 中的应用程序,以保持 return 代码一直运行,但与此同时我有一个合适的解决方法