如何调试NuGet包的install.ps1脚本

时间:2011-08-11 19:50:43

标签: c# visual-studio powershell nuget

因此,我们可以在NuGet包中包含安装/卸载powershell脚本。我试过,但我的install.ps1不起作用。有没有可能找出原因?调试,记录,什么?

更新

请注意,该脚本是作为Nuget包安装过程的一部分执行的。它可能非常具有Nuget特性。

5 个答案:

答案 0 :(得分:27)

也许我迟到了,但这是一个调试NuGet特定脚本的解决方案,NuGet包NuGetDebugTools。它的脚本Add-Debugger.ps1为NuG​​et包管理器控制台添加了一个简单而有效的调试器。

示例场景:

  • 启动Visual Studio
  • 打开NuGet控制台并输入命令

    PM> Add-Debugger [-ReadHost]
    PM> Set-PSBreakpoint -Command init
    PM> Set-PSBreakpoint -Command install
    

(或设置更具体的断点,请参阅help Set-PSBreakpoint

  • 打开Visual Studio解决方案或调用已打开的
  • 安装包XYZ
  • 调试器输入对话框出现在任何 init.ps1 install.ps1 上。
  • 输入?作为调试器输入,看看你能做什么:

    s, StepInto  Step to the next statement into functions, scripts, etc.
    v, StepOver  Step to the next statement over functions, scripts, etc.
    o, StepOut   Step out of the current function, script, etc.
    c, Continue  Continue operation (also on empty input).
    q, Quit      Stop operation and exit the debugger.
    ?, h         Display this help message.
    r            Display PowerShell command history.
    k            Display call stack (Get-PSCallStack).
    <number>     Show debug location in context of <number> lines.
    +<number>    Set location context preference to <number> lines.
    <command>    Invoke any PowerShell <command> and write its output.
    
  • 键入其他调试器和PowerShell命令并在NuGet控制台中观察输出


v1.4.0 - 新开关ReadHost告诉使用Read-Host输入而不是默认的GUI输入框。

答案 1 :(得分:8)

这就是我能够使用PowerShell ISE逐步执行install.ps1的方法:

为了能够使用PowerShell ISE逐步执行安装脚本,请按照下列步骤操作: 启用执行使用.Net 4构建的程序集

要么

C:\ Windows \ System32下\ WindowsPowerShell \ V1.0 或

C:\的Windows \ Syswow64资料\ WindowsPowerShell \ V1.0

取决于您使用的PS版本 如果文件不存在则创建它们

无论哪种 C:\ WINDOWS \ SYSTEM32 \ WindowsPowerShell \ V1.0 要么 C:\的Windows \ Syswow64资料\ WindowsPowerShell \ V1.0

取决于您使用的PS版本

如果没有配置文件,则创建它们

powershell.exe.config:

<configuration>  
    <startup useLegacyV2RuntimeActivationPolicy="true">  
        <supportedRuntime version="v4.0.30319"/>  
        <supportedRuntime version="v2.0.50727"/>  
    </startup>  
</configuration>  

powershell_ise.exe.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0.30319" />
    </startup>
</configuration>

为了能够运行NuGet包中包含的PowerShell脚本执行 政策需要改变:

Set-ExecutionPolicy RemoteSigned -Scope Process

复制要调试的install.ps1并修改其内容,如下所示:

删除参数块

param(
    [Parameter(Mandatory=$true)] [string]   $installPath,
    [Parameter(Mandatory=$true)] [string]   $toolsPath,
    [Parameter(Mandatory=$true)]            $package,
    [Parameter(Mandatory=$true)]            $project
)

导入一个允许在VS主机进程之外使用nuget cmdlet的模块

下载http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip     将bin文件夹的内容解压缩到某个位置,然后导入PackageManagement.Cmdlets.dll

像这样:

import-module "C:\dev\NuGetOutsideVisualStudio\bin\PackageManagement.Cmdlets.dll"

现在您可以手动设置所有参数:

$toolsPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4\tools"
$installPath="C:\dev\demo-solution\packages\X1.Registration.DbUpdate.0.4"

set-project DemoSolution.Logic C:\dev\demo-solution\DemoSolution.sln

$project = Get-Project -name DemoSolution.Logic

仍然没有设置$ package对象,但我发现脚本并没有真正引用该参数

参考文献: http://community.sharpdevelop.net/blogs/mattward/archive/2011/06/12/InstallingNuGetPackagesOutsideVisualStudio.aspx

答案 2 :(得分:4)

使用Set-PsDebug -trace 2查看正在发生的事情。

答案 3 :(得分:4)

通过VS中的程序包管理器控制台运行脚本(https://docs.nuget.org/ndocs/tools/package-manager-console处控制台上的详细信息) - 在此过程中导致错误的任何内容都将以红色写出。

此外,您可以使用Write-Host将诊断跟踪类型信息写入同一控制台。

答案 4 :(得分:1)

您可以在安装脚本的开头调用Start-Transcript,在结尾调用Stop-Transcript。你可能会像这样包装安装代码:

try {
  $ErrorActionPreference = 'stop'  # stop on error
  Start-Transcript c:\a.txt
  ...
}
catch {
  write-host $_
}
finally {
  Stop-Transcript
}

$ErrorActionPreference = 'inquire'(而非停止)也可能有效。但是,现在没有机会尝试。见http://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.html