0

我想我正在为一件非常简单的事情而头晕目眩。我已经研究了两天,不幸的是我无法弄清楚。

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

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

然后我注意到退出代码始终为 0,而我在脚本中对此进行了更改。然后我发现我的powershell脚本的返回码有问题,并尝试了以下方法:

测试脚本:

$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

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

Snippet 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)
4

1 回答 1

0

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

我仍在寻找一种在 MDT 中仍将 Powershell 脚本用作应用程序的方法,以保持返回代码一直正常工作,但与此同时我有一个合适的解决方法

于 2020-02-13T14:44:31.040 回答