I\'m invoking a PowerShell script from MSBuild. MSBuild is able to capture the output returned, but thinks the project is built successfully.
The problem is that th
This question is the top answer on a major search engine. The best answer is this from James Kovacs (of psake fame - i.e., he's kinda FizzBinned on PowerShell and MSBuild integration).
In summary, in a ps1 file:
$ErrorActionPreference='Stop'
at the top of your script so it's not the default, which is SilentlyContinue
(the trap
bit in the accepted answer has the same effect but is far more indirect and confusing)function exec {...
(or use psake itself)exec { x.exe }
exit ...
stuffThe default error handling propagates an exception up as an ERRORLEVEL
of 1 from the powershell.exe myscript.ps1
, i.e. in an MSBuild IgnoreExitCode="true"
and capture it with an
A final important thing to understand is that within PowerShell, there's a $?
which is the outcome of the last expression (which is not relevant if you're in ErrorAction='Stop'
mode) which changes with every thing you do, whereas $LastExitCode is the 'DOS' exit code of the last .exe triggered in the system. Details here - be sure to read the comments