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

43

我们可以在NuGet包中包含安装/卸载PowerShell脚本。我尝试过,但我的 install.ps1 脚本无法正常工作。是否有可能找出原因?调试、记录日志还是其他方法?

更新

请注意,该脚本作为NuGet包的安装过程的一部分执行。它可能非常与NuGet相关。

5个回答

27

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

更新:该脚本现在已经在PSGallery中Add-Debugger。NuGetDebugTools包不再更新。要么使用PowerShelf,要么从存储库获取各个脚本。

示例场景:

  • 启动Visual Studio

  • 打开NuGet控制台并输入命令

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

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

  • 打开Visual Studio解决方案或为已经打开的解决方案调用Install-Package XYZ

  • 当调用任何init.ps1install.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输入框。


    2
    我尝试了这个命令行调试器,结果非常棒!强烈推荐。 - Dodd
    1
    是的。在 Add-Debugger 之后,您可以定义自己的全局函数 Read-Debugger。例如,尝试这个非常简单的例子:function Read-Debugger($prompt) { Read-Host $prompt }。默认情况下不执行此操作,因为:a)该工具适用于任何主机,某些主机不实现 Read-Host;b)即使使用 Read-Host 实现,仍可能存在一些问题。所使用的对话框很傻,但它总是有效且没有问题,“除了烦恼”。 - Roman Kuzmin
    1
    抱歉,当我用手机写作时有时会太简洁了,那不是我想要的。问题只是消息框意味着我必须在消息框和控制台窗口之间分心,并且总是存在焦点问题。现在这很棒,感谢你的工作! - Voo
    2
    很抱歉,但我发现网络上的每一部分NuGet文档都非常缺乏!我的意思是它不太适合新手。我花了很多令人沮丧的时间来完成简单的任务。我已经通过包管理器控制台安装了NuGetDebugTools。然后我尝试输入:Add-Debugger,但我收到了这个错误消息:The term 'Add-Debugger' is not recognized as the name of a cmdlet, function, script file, or operable program. - QuantumHive
    1
    我不知道为什么会发生这种情况。无论如何,你不需要安装这个包。只需使用脚本即可。将其放在路径中的一个目录中或者在调用时包含路径,例如 path\Add-Debugger.ps1 - Roman Kuzmin
    显示剩余8条评论

    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 cmdlets

    下载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


    4
    使用Set-PsDebug -trace 2命令可以查看发生了什么。

    什么也不做。可能是与Nuget相关的。 - dkl
    1
    在我尝试调试PowerShell包中的cmdlet时,这对我很有用。 - Simon Gill

    4

    通过VS的“程序包管理器控制台”运行您的脚本(有关控制台的详细信息,请参见https://docs.nuget.org/ndocs/tools/package-manager-console) - 任何导致错误的内容都将以红色方式显示。

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


    “通过包管理器控制台运行脚本”是什么意思? - tofutim
    @tofutim 我添加了一个链接,指向有关包管理器控制台的信息。 - Ethan J. Brown

    1

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

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

    另外,$ErrorActionPreference = 'inquire'(而不是stop)可能也可以工作。但现在没有机会尝试。请参见http://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.html


    NuGet包不允许这样做: “安装失败。回滚中... 此主机不支持转录。” - dkl
    只有控制台主机支持转录。 - x0n

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接