Friday, November 22, 2013

Passing parameters to MSBuild

 

You may know that you can pass parameters to MSBuild to populate properties contained in a PropertyGroup.

Below is an example MSBuild file we can use for the purpose of demonstrating passing properties:

<?xml version="1.0"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0" DefaultTargets="Build">
<PropertyGroup>
<AppPoolName />
<AppPoolVersion />
</PropertyGroup>
<PropertyGroup>
<PowerShell>C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe</PowerShell>
<ScriptFilePath>H:\Scripts\AddIISAppPool.ps1</ScriptFilePath>
</PropertyGroup>
<Target Name="Build">
<Message Text="This is the value of the parameters: $(AppPoolName) and $(AppPoolVersion)" Importance="high" />
<Message Text="$(PowerShell) &amp; $(ScriptFilePath) $(AppPoolName) $(AppPoolVersion)" Importance="high" />
</Target>
</Project>


 



  1. As you may know in MSBuild v. 3.5 and above, you can also nest PropertyGroup elements inside of Target element.  Unfortunately, if you do so, you will not be able to pass those external parameters to your MSBuild script.
  2. Also, when you are passing parameters to MSBuild, make sure that you do not send the last parameter with a trailing semicolon such as: /p:AppPoolName=TestAppPool;AppPoolVersion=v4.0;. 

    If you do so, you will receive the following error message:  


    MSBUILD : error MSB1005: Specify a property and its value.
    Switch: /p:AppPoolName=TestAppPool;AppPoolVersion=v4.0;

    For switch syntax, type "MSBuild /help"

    Therefore, instead, you need to pass the parameters in this manner to avoid that error message:  /p:AppPoolName=TestAppPool;AppPoolVersion=v4.0. 


Hopefully, these words of advice can help you avoid similar problems/issues while developing and testing your MSBuild scripts.

No comments:

Post a Comment