4

我有一个 powershell 脚本,它可以在 powershell shell 和 cmd 窗口中完美运行,现在我正尝试从 Visual Studio 中的构建后步骤启动它。

经过几次不同的尝试让它吃午饭并成功运行后,我目前对 VS2010 中“构建后事件”的“命令行”属性进行了以下配置。

所有这些都在 Post-Build 配置中的一行中,但我将文本包装在这里只是为了便于阅读:

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) 中的尾随 '\' 字符会挂起吗?

4

1 回答 1

13

变量$(SolutionDir),因为它是一个以反斜杠“\”结尾的目录。因此,当变量展开时,for 的参数-SolutionDir "$(SolutionDir)"如下所示:

-SolutionDir "c:\Your Solution Dir\"

当它被解析时,最后的斜杠有效地转义了右引号,并且 shell 解析器看不到参数的结尾,因此将-SolutionDir其余参数“吸入”到 SolutionDir 参数中的参数。您可以通过添加尾部斜杠来用另一个反斜杠转义最终的反斜杠来解决此问题:

-SolutionDir "$(SolutionDir)\"

于 2012-10-04T01:31:27.047 回答