【发布时间】:2013-08-26 23:38:03
【问题描述】:
我需要使用 PowerShell -Command "& scriptname" 运行脚本,如果我从 PowerShell 返回的退出代码与脚本本身返回的退出代码相同,我会非常喜欢它。不幸的是,如果脚本返回 0,PowerShell 返回 0,如果脚本返回任何非零值,则返回 1,如下所示:
PS C:\test> cat foo.ps1
exit 42
PS C:\test> ./foo.ps1
PS C:\test> echo $lastexitcode
42
PS C:\test> powershell -Command "exit 42"
PS C:\test> echo $lastexitcode
42
PS C:\test> powershell -Command "& ./foo.ps1"
PS C:\test> echo $lastexitcode
1
PS C:\test>
使用 [Environment]::Exit(42) 几乎可以工作:
PS C:\test> cat .\baz.ps1
[Environment]::Exit(42)
PS C:\test> powershell -Command "& ./baz.ps1"
PS C:\test> echo $lastexitcode
42
PS C:\test>
除了脚本交互运行时,它会退出整个shell。有什么建议吗?
【问题讨论】:
-
我认为你想在
-command脚本中使用exit $lastexitcode退出
标签: powershell exit-code