PowerShell 2.0: One Cmdlet at a Time 59 Set-PSBreakpoint
Continuing the series looking at new cmdlets available in PowerShell 2.0. This time we look at the Set-PSBreakPoint cmdlet.
What can I do with it?
Carry out debugging by setting a breakpoint based on a condition such as line number, command or variable.
Examples:
Set a breakpoint at line 3 in the script C:\Bowling.ps1 (This is an example script taken from the 2008 Scripting Games. During the execution of the script the variable $iPoints is frequently incremented to a new value) Then run the script to utilise the breakpoint.
Set-PSBreakpoint -Script Bowling.ps1 -Line 3
You receive confirmation of the breakpoint set:
Now when you run the script, you are informed at what point we have stopped at and the current value of $iPoints. Typing Exit will leave the debugging mode.
For the next example set a breakpoint based on the variable $iPoints and carry out an action to save the value of $iPoints at that point in time into the file C:\log.txt.
Set-PSBreakpoint -Script .\Bowling.ps1 -Variable iPoints -Action {Out-File log.txt -Append -Inputobject $iPoints}
This time the confirmation shows both a Variable and an Action have been set as part of the Breakpoint.
Running the script does not bring up the interactive debugger this time.
However, the log.txt file is created and its contents show the value of $iPoints each time it is referenced in the script.
How could I have done this in PowerShell 1.0?
Setting breakpoints did not exist in PowerShell 1.0, however most scripting IDE’s ship with debugging features.