开发者

Custom powershell prompt is clearing $lastexitcode

My powershell profile has a custom powershell prompt that unfortunately causes $lastexitcode values to be lost. For instance, given a powershell script "fail.ps1" with contents "exit 123", when I run the script, $? is $false w开发者_如何学Gohile $lastexitcode is 0. If I instead run powershell without loading my profile with the custom prompt, after running fail.ps1 then $lastexitcode is 123.

Has anyone seen this problem before? Is there a way to preserve $lastexitcode as the prompt is generated?

I ran into this when using Posh-git, https://github.com/dahlbyk/posh-git, a nice powershell prompt for git.


Issue can be resolved by capturing $LASTEXITCODE at the start of the prompt and restoring it at the end:

function prompt {
    $realLASTEXITCODE = $LASTEXITCODE

    # ...

    $LASTEXITCODE = $realLASTEXITCODE
 }


You need to do this to make it work:

function prompt {
    $realLASTEXITCODE = $global:LASTEXITCODE

    # ...

    $global:LASTEXITCODE = $realLASTEXITCODE
    # cleanup
    Remove-Variable realLASTEXITCODE
 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜