MSBuild Working with the ItemGroup Command and EXEC

I created the ItemGroup shown in the code snippet. I need to iterate through this ItemGroup and run the EXEC command also shown in the code snippet. I can't seem to get it to work. The code returns the error shown below (note - the message is written 2 times, which is correct), but the EXEC command does not work correctly. The value is not set; therefore EXEC is not executed at all. I need EXEC to be executed twice or by other sections that I define in the ItemGroup.

ERROR: Encrypting WebServer Applications Section Encrypting WebServer Connection Section C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ aspnet_regiis.exe -pef "" \ gaw \ UI "-prov" RSACustomProvider "Encrypting Configuration Section ... Section configuration '' was not found.

CODE SNIPPET:

<ItemGroup>
    <SectionsToEncrypt Include="Item">
      <Section>appSettings</Section>     
    </SectionsToEncrypt>
    <SectionsToEncrypt Include="Item">     
      <Section>connectionStrings</Section>
    </SectionsToEncrypt>  
  </ItemGroup>

  <Target Name="EncryptWebServerWebConfigSections">   
    <Message Text="Encrypting WebServer %(SectionsToEncrypt.Section) section" />

    <Exec Command="$(AspNetRegIis) -pef &quot;%(SectionsToEncrypt.Section)&quot; &quot;$(DropLocation)\$(BuildNumber)\%(ConfigurationToBuild.FlavorToBuild)\$(AnythingPastFlavorToBuild)&quot; -prov &quot;$(WebSiteRSACustomProviderName)&quot;"/>
  </Target>

      

+2


a source to share


1 answer


The problem is that you are batch processing 2 items at a time. I mean you have statements

%(SectionsToEncrypt.Section)
%(ConfigurationToBuild.FlavorToBuild)

      

In the same call to the task. When you execute more than one item at a time in one task call, they will execute independently of each other. This is why you are wrong by specifying the config section `` ...

If you have FlavorToBuild, you have one value that you need to do is pass it to a property before calling Exec and then using the property. So your one liner is then converted to:



<PropertyGroup>
    <_FlavToBuild>%(ConfigurationToBuild.FlavorToBuild)<_FlavToBuild>
</PropertyGroup>
<Exec Command="$(AspNetRegIis) -pef &quot;%(SectionsToEncrypt.Section)&quot; &quot;$(DropLocation)\$(BuildNumber)\$(_FlavToBuild)\$(AnythingPastFlavorToBuild)&quot; -prov &quot;$(WebSiteRSACustomProviderName)&quot;"/>

      

If you have multiple values ​​for FlavorToBuild then it is more difficult. You would have 2 options:

  • Exec hard code more than once
  • Use targeting with batch task loading to execute foreach / foreach command

Packaging is one of the most confusing elements of MSBuild. I have compiled several online resources at http://sedotech.com/Resources#batching . If you want to know more than this, you can get a copy of my book .

+5


a source







All Articles