PowerShell脚本在Visual Studio 2010中的“Post-Build”步骤中

4
我有一个PowerShell脚本,可以在PowerShell窗口和cmd窗口中完美运行,现在我正在尝试从Visual Studio的“后期构建步骤”中启动它。
在多次尝试不同的变化来成功地启动和运行它之后,我目前在VS2010的“后期构建事件”的“命令行”属性中具有以下配置。
这全部都在“后期构建配置”中的一行上,但我在这里换行以便阅读。
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NonInteractive
-File "$(SolutionDir)testScript.ps1"
-ConfigurationFile "$(SolutionDir)configuration.txt"
-SolutionDir "$(SolutionDir)"
-Platform "$(Platform)"
-Configuration "$(Configuration)"

这个powershell脚本提供了4个必填参数:ConfigurationFile,SolutionDir,Platform和Configuration。如上所示,我已经提供了每个参数,并将它们用双引号括起来,以防止值中有空格。

在VS2010中,SolutionDir宏会扩展为带有空格的路径,Platform会扩展为“ Release”,而Configuration会扩展为“ Win32”。

当后置生成步骤执行时,我得到了以下错误:

testScript.ps1 : Cannot process command because of one or more missing
mandatory parameters: Platform Configuration.
    + CategoryInfo          : InvalidArgument: (:) [testScript.ps1], 
    ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingMandatoryParameter,testScript.ps1

你有什么想法是我可能漏掉了什么吗?它显然能在$(SolutionDir)中找到脚本,但我无法确定它是否获取了任何命令行参数。 $(SolutionDir)中的末尾'\'字符是否有问题?

1个回答

14

$(SolutionDir)是一个目录,末尾以反斜杠 '\' 结束。因此,当该变量被扩展时,-SolutionDir "$(SolutionDir)" 的参数看起来像这样:

-SolutionDir "c:\Your Solution Dir\"

当解析此内容时,最后的斜杠有效地转义了闭合引号,使得shell解析器无法看到参数的结束,因此-SolutionDir参数将把其余的参数一起“吸入”到SolutionDir参数中。您可以通过添加一个尾随斜杠来使用另一个反斜杠对最后的反斜杠进行转义,以修复此问题:

-SolutionDir "$(SolutionDir)\\ "


确保那就是它。谢谢! - Hoobajoob
过去我在将目录传递给PowerShell时遇到了很多麻烦,但不知道原因(我看到了这样的古怪行为),而这篇文章完美地解释了这一点。非常感谢! - qJake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接