powershellvisual-build-professional

Getting $LastExitCode in PowerShell when invoking Visual Build using PowerShell


I want to invoke a VisualBuild build from PowerShell and get it's last exit code.

I invoke the Build using.

Start-Process -FilePath $VisualBuild -ArgumentList "/b Somescript.bld" -PassThru -NoNewWindow

I already tried using try catch mechanism but this lead my LastExitCode to be 0 even if the Build Failed.

When using something like

$BuildProcess = Start-Process -FilePath $VisualBuild -ArgumentList "/b Somescript.bld" -PassThru -NoNewWindow
Write-Host "$($BuildProcess.ExitCode)"

my Script get's stuck after executing and displaying that the Build has failed but the Output of the LastExitCode is never displayed.

The $VisualBuild Variable holds the full Path to the VisBuildCmd.exe


Solution

  • I think you just need to get your last exit code immediately after your command and store it in a variable:

    Start-Process -FilePath $VisualBuild -ArgumentList "/b Somescript.bld" -PassThru -NoNewWindow
    $MyLastExitCode = $LastExitCode
    
    Write-Host "LastExitCode: $MyLastExitCode"
    

    I have had previous issues where I ran a command before I displayed my last exit code and it was always 0. It might be a similar issue.