【问题标题】:Unable to get msbuild and powershell to play nice with parsing a path that contains spaces无法让 msbuild 和 powershell 很好地解析包含空格的路径
【发布时间】:2015-11-23 16:33:19
【问题描述】:

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

我得到的当前错误是:

Task "Exec" (TaskId:3)
  Task Parameter:Command="C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
      "D:\Builds\23\WS\Main-SWS\Sources\Student\Main\StudentSoln\Build.ps1"
      -slnPath "D:\Builds\23\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\23\WS\Main-SWS\Sources\Student\Main\StudentSoln\Build.ps1"
  -slnPath "D:\Builds\23\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。

【问题讨论】:

  • @PetSerAl 这不起作用。它给了我一个错误,指出“D:\Builds\23\WS\Main-SWS\Sources\Student\Main\StudentSoln\Build.ps1\”不是 cmdlet、函数、脚本文件或可操作的名称程序。
  • 删除额外的\ 字符:&lt;Exec Command="&amp;quot;$(PowerShellExe)&amp;quot; &amp;quot;&amp;amp; '$(MSBuildProjectDirectory)\Build.ps1' -slnPath '$(MSBuildProjectDirectory)\' -tfExeLocation 'C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\tf.exe'&amp;quot;" /&gt;
  • @PetSerAl 没有注意到多余的斜线,所以谢谢。使用该更新再次尝试并使其正常工作。您介意将其发布为答案,以便我将其标记为答案吗?

标签: powershell visual-studio-2012 msbuild


【解决方案1】:

您的命令中的引号有问题。简单的解决方案是在 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;" />

【讨论】:

    猜你喜欢
    • 2019-08-01
    • 2015-08-05
    • 1970-01-01
    • 2015-07-21
    • 1970-01-01
    • 2018-07-20
    • 2012-02-26
    • 1970-01-01
    • 2016-11-06
    相关资源
    最近更新 更多