Preventing Debugging (Part 2)

by Aug 20, 2013

In a previous tip we explained how you can tell PowerShell not to debug certain functions by adding a special attribute:

function test {
    [System.Diagnostics.DebuggerHidden()]
    param($a)

    Get-Service


}

So when you step through code using the PowerShell debugger, the function "test" would still be executed but not stepped.

When you decorate a function with this attribute, you can still set breakpoints inside of the function, but once the debugger reached these breakpoints, you would not be able to step through the remainder of the function.

That's why there are two more alternative attributes you can use in place of DebuggerHidden: DebuggerStepThrough and DebuggerNonUserCode. They both work the same. During normal debugging, the function would still not be stepped, but when you explicitly set a breakpoint inside the function, then you could step the remaining function once the breakpoint was hit.

Twitter This Tip! ReTweet this Tip!