如何debuggingNuGet包的install.ps1脚本

所以我们可以在NuGet包中包含一个安装/卸载powershell脚本。 我试过了,但是我的install.ps1不起作用。 有没有可能找出原因? debugging,日志logging,什么?

更新

请注意,脚本是作为Nuget包安装过程的一部分执行的。 这可能是非常具体的Nuget。

也许我迟到了,但这里是一个解决scheme,用于debuggingNuGet特定的脚本,NuGet包NuGetDebugTools 。 它的脚本Add-Debugger.ps1向NuGet包pipe理器控制台添加了一个简单而有效的debugging器。

示例场景:

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

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

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

  • 打开一个Visual Studio解决scheme或调用已打开的Install-Package XYZ
  • debugging器input对话框出现在任何调用的init.ps1install.ps1上
  • 键入? 作为debugging器input,看看你能做什么:

     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. 
  • 键入其他debugging器和PowerShell命令并观察NuGet控制台中的输出


v1.4.0 – 新的交换机ReadHost告诉使用Read-Host进行input,而不是默认的GUIinput框。

这就是我使用PowerShell ISE能够逐步完成install.ps1的过程:

要能够使用PowerShell ISE逐步执行安装脚本,请执行以下步骤:启用使用.Net 4构build的程序集的执行

C:\ Windows \ System32 \ WindowsPowerShell \ v1.0或

C:\ WINDOWS \ Syswow64资料\ WindowsPowerShell \ V1.0

根据您使用的PS版本如果文件不存在,请创build它们

C:\ Windows \ System32 \ WindowsPowerShell \ v1.0或C:\ Windows \ SysWOW64 \ WindowsPowerShell \ v1.0

取决于你正在使用的PS版本

如果configuration文件不在那里创build它们

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

复制你要debugging的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

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

通过VS中的软件包pipe理器控制台( https://docs.nuget.org/ndocs/tools/package-manager-console上的控制台上的详细信息)运行脚本; – 并且将会写入导致错误的任何内容红色的。

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

您可以在安装脚本的开始处和Start-Transcript结尾处调用Start-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