Visual Studio post build事件上的PowerShell .ps1文件

时间:2011-09-02 15:19:37

标签: visual-studio-2010 powershell powershell-v2.0 nuget post-build-event

我正在尝试执行以下后期构建事件代码,但我收到一个无用的错误:

"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "$(SolutionDir)tools\nuget_pack.ps1"

我在尝试之前运行了以下PS脚本:

Set-ExecutionPolicy unrestricted

我错过了什么?

更新

这很奇怪,我现在没有在VS上收到错误。但脚本不起作用。当我使用PowerShell控制台运行它时,我收到以下错误:

enter image description here

3 个答案:

答案 0 :(得分:11)

Visual Studio将生成后事件脚本写入.BAT文件并使用cmd.exe执行该脚本。因此使用& "<path-to-powershell>"将无效。只需执行:

Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"

如果您认为您可能会在构建解决方案的其他计算机上遇到执行策略问题,请考虑采用以下方法:

Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1" 

答案 1 :(得分:7)

您可以按如下方式在Powershell中重现错误:

"this is a string" -file "my.ps1"

它将第一个作为字符串,-file作为-f格式标志,并说它没有右侧的值表达式进行格式替换。

试试这样:

<击>&安培; “c:\ Windows \ System32 \ WindowsPowerShell \ v1.0 \ powershell.exe”-file“$(SolutionDir)tools \ nuget_pack.ps1”

(正如Keith所说,这不会起作用,因为这是从一个bat文件运行而不是Powershell。)

或者只是:

powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"

答案 2 :(得分:0)

在从visual studio调用power-shell脚本之前,将ExecutionPolicy设置为不受限制的power-shell窗口,如下所示......

Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;

以下列方式调用power-shell脚本......

powershell.exe $(SolutionDir)Setup.ps1 -SolutionDir $(SolutionDir) -ProjectPath $(ProjectPath)

enter image description here

然后在脚本中,你总是可以读取像这样的参数......

param([string]$SolutionDir,
     [string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");
相关问题