7

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

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

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

Set-ExecutionPolicy unrestricted

我错过了什么?

更新

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

在此处输入图像描述

4

3 回答 3

16

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" 
于 2011-09-03T03:39:39.740 回答
8

您可以在 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"
于 2011-09-02T15:39:59.770 回答
1

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

Set-ExecutionPolicy -Scope CurrentUser;
ExecutionPolicy: unrestricted;

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

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

在此处输入图像描述

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

param([string]$SolutionDir,
     [string]$ProjectPath);
#Write-Host ($SolutionDir +" Call this script with following aruments");
#Write-Host ($ProjectPath +" Call this script with following aruments");
于 2017-01-19T09:59:38.207 回答