How to extract $lastexitcode from c# powershell script execution.

Posted by scope-creep on Stack Overflow See other posts from Stack Overflow or by scope-creep
Published on 2009-04-19T18:10:37Z Indexed on 2010/03/21 7:01 UTC
Read the original article Hit count: 1002

Filed under:
|
|
|
|

Hi, I've got a scipt executing in C# using the powershell async execution code on code project here:

http://www.codeproject.com/KB/threads/AsyncPowerShell.aspx?display=PrintAll&fid=407636&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2130851#xx2130851xx

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.

http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshost.setshouldexit(VS.85).aspx

Its really depends on defining your own exit code defintion. 0 and 1 suffixes I guess.

regards Bob.

© Stack Overflow or respective owner

Related posts about c#

Related posts about powershell