MsBuild打电话给Powershell。传递数组参数?

时间:2015-06-23 17:28:28

标签: powershell msbuild-task

我有执行PowerShell模块的msbuild任务。我调用的ps模块函数之一接受一个数组类型作为输入参数,所以我做了这样的事情:

<Exec Command="powershell -ExecutionPolicy unrestricted -command &quot;&amp; {Import-Module $(MyPowerShellModule); $(TargetFunction) -AnArray %40(&apos;OneArrayItem&apos;)}" ContinueOnError="ErrorAndContinue" />

我想要做的是能够将本机msbuild属性定义为某个值,例如,

<PSArrayValues>OneArrayItem;TwoArrayItem;ThreeArrayItem</PSArrayValues>

并且基本上可以解析我的原始目标看起来像:

<Exec Command="powershell -ExecutionPolicy unrestricted -command &quot;&amp; {Import-Module $(MyPowerShellModule); $(TargetFunction) -AnArray %40(&apos;OneArrayItem&apos,&apos;TwoArrayItem&apos,&apos;ThreeArrayItem&apos;)}" ContinueOnError="ErrorAndContinue" />

可以使用哪种方法?

1 个答案:

答案 0 :(得分:1)

您需要使用MSBuild的项目列表展平功能。通常,这会将项目组中的项目与;分开,但您可以将其更改为,,如此@(PSArrayValues, ',') /这是一个测试MSBuild项目文件,用于演示此内容:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" 
         DefaultTargets="Test" 
         ToolsVersion="4.0">

  <Target Name="Test">
    <PropertyGroup>
      <TargetFunction>Foo</TargetFunction>
    </PropertyGroup>
    <ItemGroup>
      <PSArrayValues Include="OneArrayItem"/>
      <PSArrayValues Include="TwoArrayItem"/>
      <PSArrayValues Include="ThreeArrayItem"/>
    </ItemGroup>

    <Message Text="powershell -ExecutionPolicy unrestricted -command &quot;&amp; {Import-Module $(MyPowerShellModule); $(TargetFunction) -AnArray @(PSArrayValues, ',')}&quot;" 
             Importance="High" />
  </Target>
</Project>

输出:

29> msbuild .\test.proj
Microsoft (R) Build Engine version 14.0.22823.1
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 6/23/2015 1:44:12 PM.
Project "C:\Users\hillr\test.proj" on node 1 (default targets).
Test:
  powershell -ExecutionPolicy unrestricted -command "& {Import-Module ; Foo -AnArray OneArrayItem,TwoArrayI
  tem,ThreeArrayItem}"
Done Building Project "C:\Users\hillr\test.proj" (default targets).

另请注意,您的Exec命令在最后}之后缺少结束双引号。

Build succeeded.
    0 Warning(s)
    0 Error(s)