Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSBuild in a Powershell Script - How do I know if the build succeeded?

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?

like image 943
Xavier Poinas Avatar asked Oct 24 '10 22:10

Xavier Poinas


People also ask

Is MSBuild a build tool?

MSBuild is a build tool that helps automate the process of creating a software product, including compiling the source code, packaging, testing, deployment and creating documentations. With MSBuild, it is possible to build Visual Studio projects and solutions without the Visual Studio IDE installed.

What is the path to MSBuild?

For example, the path to MSBuild.exe installed with Visual Studio 2019 Community is C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe You can also use the following PowerShell module to locate MSBuild: vssetup.

How do you build MSBuild solution?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.


2 Answers

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.

like image 60
Keith Hill Avatar answered Sep 29 '22 18:09

Keith Hill


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" } 
like image 33
Jay Bazuzi Avatar answered Sep 29 '22 18:09

Jay Bazuzi