自定义PowerShell提示符正在清除$ lastexitcode

Fra*_*man 5 powershell command-line

我的powershell配置文件有一个自定义的PowerShell提示符,不幸的是导致$ lastexitcode值丢失.例如,给定一个powershell脚本"fail.ps1",内容为"exit 123",当我运行脚本时,$?$ lastexitcode为0时为$ false.如果我在没有使用自定义提示加载我的配置文件的情况下运行powershell,则在运行fail.ps1之后,$ lastexitcode为123.

以前有人见过这个问题吗?有没有办法在生成提示时保留$ lastexitcode?

我在使用Posh-git时遇到了这个问题,https://github.com/dahlbyk/posh-git,一个很好的用于git的PowerShell提示符.

dah*_*byk 4

$LASTEXITCODE可以通过在提示开始时捕获并在结束时恢复来解决问题:

function prompt {
    $realLASTEXITCODE = $LASTEXITCODE

    # ...

    $LASTEXITCODE = $realLASTEXITCODE
 }
Run Code Online (Sandbox Code Playgroud)