首页 > 解决方案 > 如何抑制上一个命令的退出 0?

问题描述

我有两个 powershell 脚本。

script1 是一个脚本,它应该是一个包装器并将参数传递给 script2。script2 根据传递的参数生成数据。

script1 当前有一个 for 循环,它迭代如下命令:

foreach()
{   
    cmd /c "openPS.bat script2.ps1 $($someParam[-1])"
}

每次该命令成功运行时,它都会输出 exit 0

出口0

如您所见,在其中一次运行中,script2 中出现错误,我指定exit 1 + $error[0]在出现错误/异常时输出。但在 script2 中我没有指定出口 0。只有出口 1(错误)。

我阅读了| out-null在命令后使用的建议,但问题是来自 script2 的错误不再被输出/报告。

我可以说类似的话cmd /c "openPS.bat script2.ps1 $($someParam[-1]) | out-null only exit 0吗?

标签: powershell

解决方案


if((cmd /c "openPS.bat script2.ps1 $($someParam[-1])") -match "0"){Out-Null}

推荐阅读