views:

49

answers:

2

I am writting a build script with Powershell. The scripts performs various operations such as getting the latest source code off the SVN, backups, etc., and builds the solution using MSBuild:

cmd /c C:\WINDOWS\Microsoft.NET\Framework\v3.5\msbuild.exe "C:\Dev\Path\MyProjct.sln" /p:Configuration=Release 

After this instruction, I only want to execute the rest of the script if the compilation succeeded. How can I check this?

The project is a web project, so it's not so easy to check for an output, but I would guess some variables would contain the compilation result. Also since I call msbuild with cmd /c, am I going to be able to access these variables?

+5  A: 

Check the value of $LastExitCode right after the call to MSBUILD. If it is 0, then MSBUILD succeeded, otherwise it failed.

BTW there is no need to use cmd /c. Just call MSBUILD.exe directly. We do that in PowerShell build scripts all the time.

Keith Hill
+1  A: 

To just check for success/failure, use the automatic variable $?.

PS> help about_Automatic_Variables


    $?
       Contains the execution status of the last operation. It contains
    TRUE if the last operation succeeded and FALSE if it failed.

for example:

msbuild
if (! $?) { throw "msbuild failed" }
Jay Bazuzi
While `$?` works in this case, just be aware that a number of console exes (especially Microsoft tools) invert the exit code logic to return multiple success codes. So `$?` won't work on those exes.
Keith Hill