无法让 msbuild 和 powershell 很好地解析包含空格的路径

Unable to get msbuild and powershell to play nice with parsing a path that contains spaces

当在构建机器上启动构建时,我正在尝试 运行 一个简单的 powershell 脚本。据我所知,问题出在我发送的 tfExeLocation 的路径上,因为它包含空格。我尝试了几种不同的方法来转义路径,以便 msbuild 和 powershell 都对此感到满意,但我遗漏了一些东西

我得到的当前错误是:

Task "Exec" (TaskId:3)
  Task Parameter:Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
      "D:\Builds\WS\Main-SWS\Sources\Student\Main\StudentSoln\Build.ps1"
      -slnPath "D:\Builds\WS\Main-SWS\Sources\Student\Main\StudentSoln\"
      -tfExeLocation "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe"  (TaskId:3)
  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
  "D:\Builds\WS\Main-SWS\Sources\Student\Main\StudentSoln\Build.ps1"
  -slnPath "D:\Builds\WS\Main-SWS\Sources\Student\Main\StudentSoln\"
  -tfExeLocation "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe"  (TaskId:3)
  The string is missing the terminator: ". (TaskId:3)
      + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordEx  (TaskId:3)
     ception (TaskId:3)
      + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString (TaskId:3)
    (TaskId:3)
Done executing task "Exec". (TaskId:3)

这是我项目文件中目标的调用

<PropertyGroup>
  <PowerShellExe Condition="'$(PowerShellExe)'=='' ">$(WINDIR)\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
</PropertyGroup>
<Exec Command="&quot;$(PowerShellExe)&quot; &quot;$(MSBuildProjectDirectory)\Build.ps1&quot; -slnPath &quot;$(MSBuildProjectDirectory)\&quot; -tfExeLocation &quot;C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe&quot; "/>

这是我正在使用的 Powershell 脚本

Param(
    [string]$slnPath,
    [string]$tfExeLocation
    )
Push-Location "$slnPath"
write-output "Made it past push location"
& '"$tfExeLocation'" checkout *.csproj Packages.Config* /recursive
Pop-Location

非常感谢任何帮助。我很确定这很简单,因为我最近才开始使用 msbuild 组件和 Powershell。

您的命令中的引号有问题。简单的解决方案是在 PowerShell 命令中使用单引号并将所有命令放在双引号中。

"$(PowerShellExe)" "& '$(MSBuildProjectDirectory)\Build.ps1' -slnPath '$(MSBuildProjectDirectory)\' -tfExeLocation 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe'"

经过适当的 XML 转义后将变为:

<Exec Command="&quot;$(PowerShellExe)&quot; &quot;&amp; '$(MSBuildProjectDirectory)\Build.ps1' -slnPath '$(MSBuildProjectDirectory)\' -tfExeLocation 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe'&quot;" />