【问题标题】:MSBuild property expansion creates a new line in the Exec commandMSBuild 属性扩展在 Exec 命令中创建一个新行
【发布时间】:2021-09-23 11:16:08
【问题描述】:

我在某些项目中包含的目标文件中运行 powershell 脚本时遇到一些问题。

MSBuild 版本是 16.11.0.36601

以下是目标文件中的相关内容:

<PropertyGroup>
  <ScriptLocation>
    '$(SolutionDir)subdir\script.ps1'
  </ScriptLocation>
  ...
</PropertyGroup>      
<Exec Command="powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File $(ScriptLocation)" />

我的问题是在扩展属性时出现的。由于某种原因,它出现在新行上,这使命令失败。

这是输出:

0>powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File 
            'C:\code\app\subdir\script.ps1'
        
0>The command cannot be run because the File parameter requires a file path. Supply a path for the File parameter and then try the command again.

然后 script.ps1 在我的默认编辑器中打开。

所以本质上它正在运行powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File,然后继续并尝试运行下一行,即'C:\code\app\subdir\script.ps1'

我根本无法完成这项工作 - 即使我用此处记录的方法替换它:https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/running-windows-powershell-scripts-from-msbuild-project-files#executing-a-windows-powershell-script-on-the-build-server

当我这样做时 - 会发生以下情况:

0>powershell -noprofile -NonInteractive -executionpolicy Unrestricted -Command "&{ &'
            C:\code\app\subdir\script.ps1
        ' } "
0>The string is missing the terminator: '.
0>    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
0>    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
0> 
0>''' is not recognized as an internal or external command
...

有没有办法告诉 MSBuild 不要在新行上扩展属性?如果没有,有没有办法可以包装东西,这样就不会发生这种情况?

批量或等效地重写脚本并不是一个真正的选择。

【问题讨论】:

    标签: msbuild


    【解决方案1】:

    定义一个属性时,它会在该属性中包含任何换行符。

    这个:

      <ScriptLocation>
        '$(SolutionDir)subdir\script.ps1'
      </ScriptLocation>
    

    和这个不一样:

    <ScriptLocation>'$(SolutionDir)subdir\script.ps1'</ScriptLocation>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 2018-04-10
      • 2023-03-08
      • 2011-02-15
      相关资源
      最近更新 更多