gpt4 book ai didi

powershell - 从 PowerShell 脚本返回退出代码

转载 作者:行者123 更新时间:2023-12-02 22:24:59 25 4
gpt4 key购买 nike

我对从 cmd.exe 调用运行时从 PowerShell 返回退出代码值有疑问。我找到了 https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script这很有帮助。但是,PowerShell 代码的解决方案是添加一个函数。

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

但是,将“导出”放在新行上是可行的。这只是语言异常吗?
function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode)
exit }

另外,这个页面是 2010 年的。这还是现在的状态吗?现在有更好/更简单的方法吗?

最佳答案

$host.SetShouldExit($exitcode)exit是两个必须分开的命令。要么返回(如您所提到的)或分号:

function ExitWithCode { param($exitcode) $host.SetShouldExit($exitcode); exit }

关于powershell - 从 PowerShell 脚本返回退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50200325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com