基本上,我尝试将退出代码从start-process返回到脚本,以便在安装失败时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}vs
$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只是为了杀死脚本并返回错误代码。
发布于 2018-01-06 03:14:11
Start-Process将在您定义$proc时启动该进程,并且不会移动到下一行,直到它退出,因为您已经定义了-Wait参数。
这一行的if ($proc.ExitCode -ne 0) {Exit $proc.ExitCode}不会导致代码再次运行。
你可以在你的电脑上用记事本之类的快速程序运行代码来测试它,看看程序什么时候出现。
$a = start-process notepad.exe -wait -passthru
$a.exitcodehttps://stackoverflow.com/questions/48119797
复制相似问题