从 PowerShell 脚本返回退出代码 [英] Returning an exit code from a PowerShell script
问题描述
我对从 cmd.exe 调用运行时从 PowerShell 返回退出代码值有疑问.我发现 https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script 这很有帮助.但是,PowerShell 代码的解决方案是添加一个函数.
I have questions about returning an exit code value from PowerShell when run from a cmd.exe invocation. I found https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script which has been helpful. But, the solution for PowerShell code is to add a function.
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode) exit }
生成:
ExitWithCode : The term 'ExitWithCode' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\src\t\e\exit5.ps1:6 char:1
+ ExitWithCode -exitcode 12345
+ ~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (ExitWithCode:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
但是,将退出"放在新行上是可行的.这只是语言异常吗?
But, placing the "exit" on a new line works. Is this just a language anomaly?
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit }
另外,这个页面是 2010 年的.这还是现在的状态吗?现在有更好/更简单的方法吗?
Also, this page is from 2010. Is this still the current state? Is there a better/easier way now?
推荐答案
$host.SetShouldExit($exitcode)
和 exit
是两个必须分开的命令.要么带回车(就像你提到的那样)或带分号:
$host.SetShouldExit($exitcode)
and exit
are two commands which have to separated. either with a return (like you mentioned) or with a semicolon:
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode); exit }
这篇关于从 PowerShell 脚本返回退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!