我需要使用PowerShell命令“& scriptname”运行脚本,如果退出PowerShell的退出代码与退出代码相同,我真的很喜欢它脚本本身返回的退出代码。不幸的是,如下图所示PowerShell会返回0,如果脚本返回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>
使用[环境] ::出口(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'脚本 – ProfessionalAmateur