繁体   English   中英

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

[英]PowerShell .ps1 file on Visual Studio post build event

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

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

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

Set-ExecutionPolicy unrestricted

我错过了什么?

UPDATE

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

在此输入图像描述

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" 

您可以在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"

在从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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM