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

tug*_*erk 5 powershell visual-studio-2010 powershell-2.0 post-build-event nuget

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

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

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

Set-ExecutionPolicy unrestricted
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

UPDATE

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

在此输入图像描述

Kei*_*ill 12

Visual Studio将生成后事件脚本写入.BAT文件并使用cmd.exe执行该操作.所以使用& "<path-to-powershell>"不起作用.只需执行:

Powershell.exe -file "$(SolutionDir)tools\nuget_pack.ps1"
Run Code Online (Sandbox Code Playgroud)

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

Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1" 
Run Code Online (Sandbox Code Playgroud)


man*_*lds 7

您可以在Powershell中重现错误,如下所示:

"this is a string" -file "my.ps1"
Run Code Online (Sandbox Code Playgroud)

它将第一个作为字符串,-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"
Run Code Online (Sandbox Code Playgroud)