2012-10-04 52 views
4

我有一個PowerShell腳本,可以從PowerShell的shell和cmd窗口中工作,現在我正嘗試從Visual Studio中的後期構建步驟啓動它。Visual Studio 2010中的Powershell腳本構建後步驟

經過幾種不同的嘗試將此設置爲午餐併成功運行之後,我目前對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,平臺,和配置。正如你所看到的,我提供了上面的每個參數,並用雙引號括起來以防止值中的空格。

在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)中尾隨的'\'字符是否會掛起?

回答

13

變量$(SolutionDir),因爲它是一個目錄以反斜槓'\'結尾。所以,當變量被擴展,爲-SolutionDir "$(SolutionDir)"的說法是這樣的:

-SolutionDir "c:\Your Solution Dir\" 

當這個被解析最後的斜槓有效逃脫關閉的引號和shell解釋不看參數的結尾,所以參數-SolutionDir將剩餘參數「吸入」到SolutionDir參數中。您可以通過添加一個尾隨斜線逃避與另一個決賽反斜線解決這個問題:

-SolutionDir「$(SolutionDir)\」

+0

當然作爲亂射就是這樣。謝謝! – Hoobajoob

+0

我在過去不知道爲什麼(我看到古怪的行爲是這樣的)時,在傳入Powershell的目錄中遇到了很多麻煩,而這完全解釋了它。 **謝謝!** – qJake

相關問題