1

基本上,我试图将退出代码从启动进程返回到脚本,以便在安装失败时 MDT/SCCM 可以正确失败。

通常这里是我正在使用的代码:

$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -Passthrough
Exit $proc.ExitCode 

我的问题是什么时候Start-Process执行?当我定义 $proc 或调用时$proc.ExitCode

我要做的是在if语句中使用退出代码,而不必将该代码存储在另一个变量中(减少代码混乱)。

$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -PassThru
if ($proc.ExitCode -ne 0) {Exit $proc.ExitCode}

$proc2 = Start-Process -FilePath $setupexe2 -ArgumentList $setupargs2 -Wait -PassThru
if ($proc2.ExitCode -ne 0) {Exit $proc.ExitCode}

对比

$proc = Start-Process -FilePath $setupexe -ArgumentList $setupargs -Wait -PassThru
$procexit = $proc.ExitCode
if ($procexit -ne 0) {Exit $procexit}

$proc2 = Start-Process -FilePath $setupexe2 -ArgumentList $setupargs2 -Wait -PassThru
$procexit2 - $proc2.ExitCode
if ($procexit2 -ne 0) {Exit $procexit2}

我不希望Start-Process仅仅为了杀死脚本并返回错误代码而再次调用。

4

1 回答 1

1

Start-Process将在您定义时启动该过程,$proc并且不会移动到下一行,直到它退出,因为您已经-Wait定义了参数。

此行if ($proc.ExitCode -ne 0) {Exit $proc.ExitCode}不会导致代码再次运行。

您可以通过使用记事本等快速程序运行代码在您的计算机上进行测试,并查看程序何时出现。

$a = start-process notepad.exe -wait -passthru
$a.exitcode
于 2018-01-05T19:14:11.127 回答