如何从 C# powershell 脚本执行中提取 $lastexitcode
我在此处的代码项目上使用 powershell 异步执行代码在 C# 中执行了一个 scipt:
我需要返回 $lastexitcode 和 Jean-Paul 描述如何使用自定义 pshost 类来返回它。 我在 pshost 中找不到任何返回退出代码的方法或属性。
我的这个引擎需要确保脚本正确执行。
任何帮助,将不胜感激。
问候 鲍勃.
它是 $lastexitcode 和 $? 我需要带回来的变量。
你好, 终于有答案了。
我发现了 $host 变量。 它实现了对主机的回调,特别是自定义 PSHost 对象,使您能够返回 $lastexitcode。 这是 $host 解释的链接。
http://mshforfun.blogspot.com /2006/08/do-you-know-there-is-host-variable.html
与 powershell 文档一样,它似乎晦涩难懂,文档记录很差。 使用第 4 点,调用 $host.SetShouldExit(1) 将 1 返回到 pshost 的 SetShouldExit 方法,如此处所述。
它实际上取决于定义您自己的退出代码定义。 我猜是 0 和 1 后缀。
问候 鲍勃.
I've got a scipt executing in C# using the powershell async execution code on code project here:
I need to return the $lastexitcode and Jean-Paul describes how you can use a custom pshost class to return it. I can't find any method or property in pshost that returns the exit code.
This engine I have needs to ensure that script executes correctly.
Any help would be appreciated.
regards
Bob.
Its the $lastexitcode and the $? variables I need to bring back.
Hi,
Finally answered.
I found out about the $host variable. It implements a callback into the host, specifically a custom PSHost object, enabling you to return the $lastexitcode. Here is a link to an explanation of $host.
http://mshforfun.blogspot.com/2006/08/do-you-know-there-is-host-variable.html
It seems to be obscure, badly documented, as usual with powershell docs. Using point 4, calling $host.SetShouldExit(1) returns 1 to the SetShouldExit method of pshost, as described here.
Its really depends on defining your own exit code defintion. 0 and 1 suffixes I guess.
regards
Bob.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是您可以尝试的功能:
希望有帮助
Here is a function you can try:
Hope that helps
您可以编写脚本代码来检查 $lastexitcode 并抛出异常 如果退出代码不是您排除的代码。
异常更容易捕获。
You can write in your script code that will check the $lastexitcode and will throw an exception if the exitcode is not what you excepted.
Exceptions are easier to catch.
我相信您使用此代码项目是小题大做。 在 C# 中异步执行非常容易。
另外,查看您对该项目的问题,您似乎正在尝试在 PowerShell 中使用 TFS。 您可以考虑以下附加信息:
希望这可以帮助
I believe you are making a mountain out of a molehill by using this code project. Asynchronous execution is very easy to do in C#.
Also, looking at your question on that project, it looks like you are trying to use TFS in PowerShell. You can consider the following additional pieces of information:
Hope this helps