从PowerShell脚本中接收错误级别


本文的脚本演示如何让PowerShell给调用者回传一个数字值,作为状态码:

$exitcode = 123
$p = Start-Process -FilePath powershell -ArgumentList "-command get-process; exit $exitcode" -PassThru
Wait-Process -Id $p.Id
'External Script ended with exit code ' + $p.ExitCode

如果你直接在PowerShell调用这段脚本(不使用Start-Process),数字格式的状态码会被暴露在$LASTEXITCODE 中。

$exitcode = 199
powershell.exe "get-process; exit $exitcode"
'External Script ended with exit code ' + $LASTEXITCODE

但是如果你在批处理或着VBScript中运行这段脚本,数字值,可以在%ERRORLEVEL%中接收到,有点像PowerShell只是又给控制台应用程序,事实也是如此。

原文链接:Receiving Error Level from PowerShell Script

本文链接: https://www.pstips.net/receiving-error-level-from-powershell-script.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注