【问题标题】:PowerShell .ps1 file on Visual Studio post build eventVisual Studio 生成后事件上的 PowerShell .ps1 文件
【发布时间】:2011-09-02 15:19:37
【问题描述】:

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

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

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

Set-ExecutionPolicy unrestricted

我错过了什么?

更新

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

【问题讨论】:

  • 无用的错误?如果没有一些线索作为您遇到的问题,几乎不可能回答。
  • @Jamiec 查看更新后的问题。
  • @Jamiec 这就是答案。谢谢!

标签: visual-studio-2010 powershell powershell-2.0 nuget post-build-event


【解决方案1】:

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" 

【讨论】:

  • 我的错,忘记了 bat 以及 OP 在 Powershell 控制台中尝试过它。那么文件部分是否应该是$($SolutionDir)(我将它从OP 最初认为它是PS 的部分改变了),但我想VS / MSBuild 会以不同的方式需要它? $(SolutionDir)?
  • 没什么大不了的。是的,应该是$(SolutionDir)。这就是您在 MSBuild 中引用属性的方式。固定的。谢谢。
  • 根据:technet.microsoft.com/en-us/library/hh847736.aspx“文件必须是命令中的最后一个参数”所以它应该是:Powershell.exe -ExecutionPolicy Unrestricted -file "$(SolutionDir)tools\nuget_pack.ps1" BTW .多亏了这一点,您可以将参数添加到 ps1
  • @AndrzejMartyna 不错。更新了答案。谢谢。
【解决方案2】:

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

【讨论】:

  • 所以问题出在“c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe”上?
  • @tugberk - 它被视为字符串。请参阅我更新的答案,为您提供尝试的建议。
  • 我可以让它工作,直到我有带空格的参数。我如何用带空格的参数来调用它?
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    相关资源
    最近更新 更多