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

dkl*_*dkl 43 c# powershell visual-studio nuget

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

更新

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

Rom*_*min 27

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

示例场景:

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

    PM> Add-Debugger [-ReadHost]
    PM> Set-PSBreakpoint -Command init
    PM> Set-PSBreakpoint -Command install
    
    Run Code Online (Sandbox Code Playgroud)

(或设置更具体的断点,请参阅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.
    
    Run Code Online (Sandbox Code Playgroud)
  • 键入其他调试器和PowerShell命令,并在NuGet控制台中观察输出


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

  • 我试过这个cmdline调试器,结果很精彩!强烈推荐. (2认同)
  • 对不起,但我发现我在网上看到的每个部分的NuGet文档非常稀少!有了这个,我的意思是它不是非常友好的诺布.我发现在完成简单任务时遇到了许多令人沮丧的时间.我已经通过Package Manager Console安装了NuGetDebugTools.然后我尝试输入:`Add-Debugger`但是我收到此错误消息:`术语'Add-Debugger'未被识别为cmdlet,函数,脚本文件或可操作程序的名称. (2认同)

amb*_*ous 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>  
Run Code Online (Sandbox Code Playgroud)

powershell_ise.exe.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
      <supportedRuntime version="v4.0.30319" />
    </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

为了能够运行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
)
Run Code Online (Sandbox Code Playgroud)

导入一个允许在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"
Run Code Online (Sandbox Code Playgroud)

现在您可以手动设置所有参数,如下所示:

$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
Run Code Online (Sandbox Code Playgroud)

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

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