13

I have a powershell script and I load a dll via [Reflection.Assembly]::Load

I want to place brakepoints into the source code of that dll, add watches etc.

Attaching to the powershell process didn't work (actually I tried the powershell ise). There are no other processes to attach to. Any ideas? Once an exception (it's my exception, so this supposed to happen) appeared in VS but I couldn't reproduce it.

naeron84
  • 2,955
  • 3
  • 30
  • 37
  • 2
    I've certainly debugged C# code by attaching to the powershell.exe process. I can't recall ever doing so with the ISE, but I don't know why that wouldn't work too. – OldFart Nov 09 '11 at 15:33

1 Answers1

9

As an alternative, you could create an helper class in your library:

namespace Something {
    public static class DebugHelper {
        public static void AttachDebugger() {
            System.Diagnostics.Debugger.Launch();
        }
    }
}

Then, you can call that method from PowerShell, and you will get the debugger attached.

Paolo Tedesco
  • 55,237
  • 33
  • 144
  • 193
  • 3
    I noticed that sometimes it doesn't work. VS Launches but the exception doesn't get caught. Note that I used this line from power shell directly: [System.Diagnostics.Debugger]::Launch() – naeron84 Nov 09 '11 at 13:19
  • 2
    My idea was to use [Something.DebugHelper]::AttachDebugger(), so you have the debugger in your code, and then it's easy to add breakpoints etc, but probably your solution is even simpler. – Paolo Tedesco Nov 09 '11 at 13:26