【问题标题】:MSBuild: Passing multiple parameters to powershell not working as expectedMSBuild:将多个参数传递给 powershell 未按预期工作
【发布时间】:2015-09-18 16:35:54
【问题描述】:

我正在使用此代码从 MSBuild 目标调用 PowerShell 脚本:

    <PropertyGroup>
    <enabler/>
</PropertyGroup>    
<Target Name="testTarget">
    <CreateItem Include="LogsGeneratorScript.ps1">
        <Output ItemName="LogsGenerator" TaskParameter="Include" />
    </CreateItem>

    <ItemGroup>
        <Enabling Include="$(enabler)" />
    </ItemGroup> 

    <PropertyGroup>
        <PowerShellExe Condition=" '$(PowerShellExe)'=='' "> 
  %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted  -command "&amp; {  &amp;'%(LogsGenerator.Fullpath)'  '$(MSBuildProjectName)' '%(Enabling.Identity)'} "
        </PowerShellExe>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe)" />
</Target>

我遇到的问题是 %(Enabling.Identity) 导致 %(LogsGenerator.Fullpath) 被作为空白传递,所以我可以看到当调用 PowerShell 时,整个事情看起来像:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& { &'' 'MyTestProject' 'true'} "

我期待这个:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& { &'LogsGeneratorScript.ps1' 'MyTestProject' 'true'} "

请注意,如果我删除最后一个参数%(Enabling.Identity),那么第一个百分比符号属性将被正确解析:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive -executionpolicy Unrestricted -command "& { &'LogsGeneratorScript.ps1' 'MyTestProject'} "

有人可以建议一种方法,以便我可以将两个百分比符号参数都传递给 PowerShell 吗?

【问题讨论】:

  • 你能告诉我们更多的上下文,包括项目定义吗?我怀疑问题更多在于您如何使用 MSBuild 转换而不是 powershell。
  • @mikez 我已经更新了问题。

标签: powershell msbuild


【解决方案1】:

问题将是 MSBuild 独立地迭代两个 ItemGroups,如 here 所述;整个第一个 ItemGroup(忽略第二个),然后是整个第二个 ItemGroup(忽略第一个)。

这就是您看到%(LogsGenerator.Fullpath) 空白的原因;它正忙于迭代%(Enabling.Identity)

我怀疑您的 PowerShell 命令将被调用两次 - 一次如您所描述,一次设置 %(LogsGenerator.Fullpath)%(Enabling.Identity) 空白。

因此,您需要决定如何组合这些组。有一个解释 here 关于如何迭代两个 ItemGroups 的 Cartesian product

或者,您不能通过在某处使用condition 而不是将enabler 转换为ItemGroup 来完全避免这种情况吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2018-08-05
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    相关资源
    最近更新 更多