43

So we can include an install/uninstall powershell scripts in a NuGet package. I tried, but my install.ps1 does not work. Is there any possibility to find out why? Debugging, logging, anything?

Update

Please note that the script is executed as part of an installation process of Nuget package. It may be very Nuget-specific.

Meligy
  • 35,654
  • 11
  • 85
  • 109
dkl
  • 3,850
  • 2
  • 27
  • 26

5 Answers5

27

Perhaps I am late to the party but here is a solution for debugging NuGet specific scripts, the NuGet package NuGetDebugTools. Its script Add-Debugger.ps1 adds a simple and yet effective debugger to the NuGet package manager console.

UPDATE: The script is now at PSGallery Add-Debugger. The NuGetDebugTools package is no longer updated. Either use PowerShelf or get individual scripts from the repository.

The sample scenario:

  • start Visual Studio

  • open NuGet console and type commands

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

(or set more specific breakpoints, see help Set-PSBreakpoint)

  • open a Visual Studio solution or invoke Install-Package XYZ for already opened

  • the debugger input dialog appears on any init.ps1 and install.ps1 invoked

  • type ? as debugger input and see what you can do:

      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.
    
  • type other debugger and PowerShell commands and watch the output in the NuGet console


v1.4.0 - New switch ReadHost tells to use Read-Host for input instead of the default GUI input box.

Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
  • 2
    I tried this cmdline debugger, which turned out to be wonderful! Highly recommended. – Dodd Jul 18 '14 at 03:47
  • Is there any way to *not* get the message box but instead type commands into the console? Apart from that annoyance, these scripts are great. – Voo Jun 19 '15 at 10:19
  • 1
    Yes. After `Add-Debugger` you can define your own global function `Read-Debugger`. For example, try this very straightforward: `function Read-Debugger($prompt) { Read-Host $prompt }`. This is not done by default because: a) the tool is for any host, some hosts do not implement `Read-Host`; b) even with `Read-Host` implemented some issues are possible. The used dialog box is silly but it just works, always and without issues, "apart from annoyance". – Roman Kuzmin Jun 19 '15 at 10:55
  • **v1.4.0** - New switch `ReadHost` tells to use `Read-Host` for input instead of the default GUI input box. – Roman Kuzmin Jun 20 '15 at 14:03
  • 1
    Sorry when writing on my phone I'm sometimes a bit too terse, that didn't come across as I wanted it. The problem is just that the message box means I have to split my attention between the message box and the console window and there's always the problem with focus. This is just great now, thanks for your work! – Voo Jun 22 '15 at 08:55
  • 2
    I'm sorry, but I find that the NuGet documentation from every part I've read on the net is very scarce! With this I mean that it's not very noob friendly. I find many frustrating hours in trying to get simple tasks done. I've installed the NuGetDebugTools via Package Manager Console. Then I try to enter: `Add-Debugger` but I'm getting this error message: `The term 'Add-Debugger' is not recognized as the name of a cmdlet, function, script file, or operable program.` – QuantumHive Mar 16 '16 at 16:23
  • 1
    I do not know why this is happening. In any case, you do not have to install the package. Just use the script. Either put it in a directory included in the path or invoke it with the path included, i.e. `path\Add-Debugger.ps1`. – Roman Kuzmin Mar 16 '16 at 19:45
  • Add-Debugger is failing. I am getting the same thing as QuantumHive. I had to run ".\packages\NuGetDebugTools.1.4.1\tools\Add-Debugger.ps1" but with the full path it worked. – Rhyous Apr 28 '16 at 16:52
  • @Rhyous If it worked with the full path then there is no problem with the script. Probably NuGet changed something. – Roman Kuzmin Apr 28 '16 at 17:18
  • I have tried this right now in VS2015 and it worked. – Roman Kuzmin Apr 28 '16 at 17:25
  • BTW, why do you run ".\packages\NuGetDebugTools.1.4.1\tools\Add-Debugger.ps1"? You should just run "Add-Debugger.ps1". – Roman Kuzmin Apr 28 '16 at 17:34
  • Because when I run Add-Debugger.ps1 without the full path it fails as an unrecognized cmd-let. – Rhyous Apr 28 '16 at 17:50
  • If you want to solve this problem with my help then file the issue at the project site with some more details. It works for me fine in VS2015 and my NuGet install. Something is not quite right with your setup, presumably. – Roman Kuzmin Apr 29 '16 at 03:09
8

This is how I was able to step-through install.ps1 using PowerShell ISE:

To be able to step through execution of the install script using PowerShell ISE follow these steps: Enable execution of assemblies built with .Net 4

Either

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

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

Depending on which version of PS you're using If files are not there create them

Either C:\Windows\System32\WindowsPowerShell\v1.0 Or C:\Windows\SysWOW64\WindowsPowerShell\v1.0

Depending on which version of PS you're using

If config files are not there create them

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>

To be able to run PowerShell scripts included with a NuGet package the execution policy will need to be changed:

Set-ExecutionPolicy RemoteSigned -Scope Process

Copy install.ps1 that you want to debug and modify it's contents as follows:

delete the parameters block

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

import a module which allows to use nuget cmdlets outside of the VS host process

Download http://community.sharpdevelop.net/blogs/mattward/NuGet/NuGetOutsideVisualStudio.zip Extract contents of the bin folder to some place and then import the PackageManagement.Cmdlets.dll

like so:

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

now you are able to set all the parameters manually like so:

$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

That still leaves $package object unset but I found that script doesn't really refer to that parameter

References: http://community.sharpdevelop.net/blogs/mattward/archive/2011/06/12/InstallingNuGetPackagesOutsideVisualStudio.aspx

ambidexterous
  • 832
  • 12
  • 21
4

Use Set-PsDebug -trace 2 to see what is happening.

manojlds
  • 290,304
  • 63
  • 469
  • 417
4

Run your scripts through the Package Manager Console in VS (details on the console at https://docs.nuget.org/ndocs/tools/package-manager-console) -- and anything that causes an error along the way will be written out in red.

Also, you can write diagnostic trace type info with Write-Host to the same console.

Ethan J. Brown
  • 2,308
  • 3
  • 20
  • 27
1

You might call Start-Transcript at the beginning of install script and Stop-Transcript at the end. You would probably wrap the install code like this:

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

Also $ErrorActionPreference = 'inquire' (instead of stop) could possibly work. However, no chance to try it now. See http://tasteofpowershell.blogspot.com/2008/07/handling-errors-in-powershell.html

stej
  • 28,745
  • 11
  • 71
  • 104
  • This is not allowed for NuGet packages: " Install failed. Rolling back... This host does not support transcription. " – dkl Aug 11 '11 at 20:40
  • Only the console host supports transcription. – x0n Dec 21 '11 at 04:09