Debug .NET Assembly In PowerShell ISE

In this blog, we will look into debugging the .NET code in PowerShell. PowerShell is an object-oriented programming language with interactive command line shell for Windows. It is used to automate system tasks, such as batch processing, and create system management tools for commonly implemented administrative tasks. We can use .NET methods written in an assembly within our PowerShell script. Sometimes, we want to debug that .NET code from PowerShell. To do that, we need to add System.Diagnostics.Debugger.Launch(); in the .NET code for launching the debugger.

Let’s understand it with the below example. Create a class library with a method to divide two numbers.

class

Let’s load this assembly in PowerShell and run it.

assembly

Here, we are loading assembly using LoadFile method, and printing the output of DivideMethod(). Here, we are dividing by zero, which will throw an exception.

Let’s debug it by adding System.Diagnostics.Debugger.Launch(); statement in our DivideMethod(), as shown below:

statement

Run it (F5) within PowerShell ISE. This will launch the debugger to debug .NET code:

debugger

code

By following the above steps, we can debug the .NET code within PowerShell script.

I am ending things here. I hope this blog will be helpful for all.

Ebook Download
View all
Learn
View all