honzajscz
honzajscz

Reputation: 3107

Debugger statement in PowerShell

In JavaScript there is the debugger; statement that stops execution and acts as a break point. In C# there is the Debugger.Break() method with almost identical behavior. Does in PowerShell exist something similar?

Upvotes: 4

Views: 719

Answers (2)

Patrick Meinecke
Patrick Meinecke

Reputation: 4173

Wait-Debugger is the PowerShell equivalent.

Upvotes: 8

honzajscz
honzajscz

Reputation: 3107

So I came up with this simple PowerShell function that does the job

Function Set-PSBreakPointHere {

  $line = [int] $Myinvocation.ScriptlineNumber + 1 # following line
  $script = $Myinvocation.PSCommandPath

  # set a break point that pauses execution 
  Set-PSBreakpoint -Line $line -Script $script |  Out-Null

  # set a break point that removes the previous break and itself 
  Set-PSBreakpoint -Line $line -Script $script -Action { $breakPOint = Get-PSBreakpoint -Script $script | Where Line -EQ $line | Remove-PSBreakpoint }.GetNewClosure() | Out-Null

}

This function creates two break points on the line at the script where it is invoked. One of the breakpoints stops the execution and enters the debugger (that is what I wanted) and the other breakpoint unregisters both of these break points to keep things clean.

Upvotes: 1

Related Questions