How to Debug Scripts in Windows PowerShell ISE. 9 minutes to read.In this articleThis article describes how to debug scripts on a local computer by using the Windows PowerShellIntegrated Scripting Environment (ISE) visual debugging features. How to manage breakpointsA breakpoint is a designated spot in a script where you would like operation to pause so that youcan examine the current state of the variables and the environment in which your script is running.Once your script is paused by a breakpoint, you can run commands in the Console Pane to examine thestate of your script. You can output variables or run other commands. You can even modify the valueof any variables that are visible to the context of the currently running script.
![]()
After you haveexamined what you want to see, you can resume operation of the script.You can set three types of breakpoints in the Windows PowerShell debugging environment:.Line breakpoint. The script pauses when the designated line is reached during the operationof the script.Variable breakpoint.
The script pauses whenever the designated variable's value changes.Command breakpoint. The script pauses whenever the designated command is about to be runduring the operation of the script. It can include parameters to further filter the breakpoint toonly the operation you want. The command can also be a function you created.Of these, in the Windows PowerShell ISE debugging environment, only line breakpoints can be set byusing the menu or the keyboard shortcuts. The other two types of breakpoints can be set, but theyare set from the Console Pane by using thecmdlet. This section describes how you can perform debugging tasks in Windows PowerShell ISE byusing the menus where available, and perform a wider range of commands from the Console Pane byusing scripting. To set a breakpointA breakpoint can be set in a script only after it has been saved.
Script Debugger 7.0.6 macOS 45 mbScript Debugger provides everything you need to quickly and easily author AppleScripts that work. No other scripting tool. No other scripting tool can match Script Debugger’s capabilities for creating, editing and debugging AppleScripts. Its sophisticated Dictionary browser provides far more information than any other tool can and eliminates guesswork and needless experimentation. Simply put, Script Debugger makes AppleScript easy.
Right-click the line where youwant to set a line breakpoint, and then click Toggle Breakpoint. Or, click the line where youwant to set a line breakpoint, and press F9 or, on the Debug menu, click Toggle Breakpoint.The following script is an example of how you can set a variable breakpoint from the Console Pane byusing thecmdlet. # This command sets a breakpoint on the Server variable in the Sample.ps1 script.Set-PSBreakpoint -Script sample.ps1 -Variable ServerList all breakpointsDisplays all breakpoints in the current Windows PowerShell session.On the Debug menu, click List Breakpoints. The following script is an example of how you canlist all breakpoints from the Console Pane by using thecmdlet.
# This command lists all breakpoints in the current session.Get-PSBreakpointRemove a breakpointRemoving a breakpoint deletes it.If you think you might want to use it again later, considerit instead. Right-click the line where you want to remove a breakpoint, and then click ToggleBreakpoint.Or, click the line where you want to remove a breakpoint, and on the Debug menu, clickToggle Breakpoint.
![]()
The following script is an example of how to remove a breakpoint with aspecified ID from the Console Pane by using thecmdlet. # This command deletes the breakpoint with breakpoint ID 2.Remove-PSBreakpoint -Id 2Remove All BreakpointsTo remove all breakpoints defined in the current session, on the Debug menu, click Remove All Breakpoints.The following script is an example of how to remove all breakpoints from the Console Pane by usingthecmdlet. # This command deletes all of the breakpoints in the current session.Get-PSBreakpoint Remove-PSBreakpointDisable a BreakpointDisabling a breakpoint does not remove it; it turns it off until it is enabled. To disable aspecific line breakpoint, right-click the line where you want to disable a breakpoint, and thenclick Disable Breakpoint. Or, click the line where you want to disable a breakpoint, and pressF9 or, on the Debug menu, click Disable Breakpoint. The following script is anexample of how you can remove a breakpoint with a specified ID from the Console Pane by using thecmdlet. # This command disables the breakpoint with breakpoint ID 0.Disable-PSBreakpoint -Id 0Disable All BreakpointsDisabling a breakpoint does not remove it; it turns it off until it is enabled.
To disable allbreakpoints in the current session, on the Debug menu, click Disable all Breakpoints. Thefollowing script is an example of how you can disable all breakpoints from the Console Pane by usingthecmdlet. # This command disables all breakpoints in the current session.# You can abbreviate this command as: 'gbp dbp'.Get-PSBreakpoint Disable-PSBreakpointEnable a BreakpointTo enable a specific breakpoint, right-click the line where you want to enable a breakpoint, andthen click Enable Breakpoint. Or, click the line where you want to enable a breakpoint, and thenpress F9 or, on the Debug menu, click Enable Breakpoint. The following script is anexample of how you can enable specific breakpoints from the Console Pane by using thecmdlet.
# This command enables breakpoints with breakpoint IDs 0, 1, and 5.Enable-PSBreakpoint -Id 0, 1, 5Enable All BreakpointsTo enable all breakpoints defined in the current session, on the Debug menu, click Enable allBreakpoints. The following script is an example of how you can enable all breakpoints from theConsole Pane by using thecmdlet. # This command enables all breakpoints in the current session.# You can abbreviate the command by using their aliases: 'gbp ebp'.Get-PSBreakpoint Enable-PSBreakpointHow to manage a debugging sessionBefore you start debugging, you must set one or more breakpoints. You cannot set a breakpoint unlessthe script that you want to debug is saved.
For directions on of how to set a breakpoint, seeor.After you start debugging, you cannot edit a script until you stop debugging. A script that has oneor more breakpoints set is automatically saved before it is run. To start debuggingPress F5 or, on the toolbar, click the Run Script icon, or on the Debug menuclick Run/Continue.
The script runs until it encounters the first breakpoint. It pausesoperation there and highlights the line on which it paused.
To continue debuggingPress F5 or, on the toolbar, click the Run Script icon, or on the Debug menu,click Run/Continue or, in the Console Pane, type C and then press ENTER. This causes thescript to continue running to the next breakpoint or to the end of the script if no furtherbreakpoints are encountered. To view the call stackThe call stack displays the current run location in the script. If the script is running in afunction that was called by a different function, then that is represented in the display byadditional rows in the output.
The bottom-most row displays the original script and the line in itin which a function was called. The next line up shows that function and the line in it in whichanother function might have been called. The top-most row shows the current context of the currentline on which the breakpoint is set.While paused, to see the current call stack, press CTRL+ SHIFT+ D or, on the Debug menu, clickDisplay Call Stack or, in the Console Pane, type K and then press ENTER.
To stop debuggingPress SHIFT+ F5 or, on the Debug menu, click Stop Debugger, or, in theConsole Pane, type Q and then press ENTER. How to step over, step into, and step out while debuggingStepping is the process of running one statement at a time. You can stop on a line of code, andexamine the values of variables and the state of the system.
The following table describes commondebugging tasks such as stepping over, stepping into, and stepping out. Debugging TaskDescriptionHow to accomplish it in PowerShell ISEStep IntoExecutes the current statement and then stops at the next statement. If the current statement is a function or script call, then the debugger steps into that function or script, otherwise it stops at the next statement.Press F11 or, on the Debug menu, click Step Into, or in the Console Pane, type S and press ENTER.Step OverExecutes the current statement and then stops at the next statement.
If the current statement is a function or script call, then the debugger executes the whole function or script, and it stops at the next statement after the function call.Press F10 or, on the Debug menu, click Step Over, or in the Console Pane, type V and press ENTER.Step OutSteps out of the current function and up one level if the function is nested. If in the main body, the script is executed to the end, or to the next breakpoint.
The skipped statements are executed, but not stepped through.Press SHIFT+ F11, or on the Debug menu, click Step Out, or in the Console Pane, type O and press ENTER.ContinueContinues execution to the end, or to the next breakpoint. The skipped functions and invocations are executed, but not stepped through.Press F5 or, on the Debug menu, click Run/Continue, or in the Console Pane, type C and press ENTER.How to display the values of variables while debuggingYou can display the current values of variables in the script as you step through the code. To display the values of standard variablesUse one of the following methods:.In the Script Pane, hover over the variable to display its value as a tool tip.In the Console Pane, type the variable name and press ENTER.All panes in ISE are always in the same scope. Therefore, while you are debugging a script, thecommands that you type in the Console Pane run in script scope. This allows you to use the ConsolePane to find the values of variables and call functions that are defined only in the script. To display the values of automatic variablesYou can use the preceding method to display the value of almost all variables while you aredebugging a script.
However, these methods do not work for the following automatic variables.$.$Input.$MyInvocation.$PSBoundParameters.$ArgsIf you try to display the value of any of these variables, you get the value of that variable for inan internal pipeline the debugger uses, not the value of the variable in the script. You can workaround this for a few variables ( $, $Input, $MyInvocation, $PSBoundParameters, and $Args) by usingthe following method:.In the script, assign the value of the automatic variable to a new variable.Display the value of the new variable, either by hovering over the new variable in the ScriptPane, or by typing the new variable in the Console Pane.For example, to display the value of the $MyInvocation variable, in the script, assign the value toa new variable, such as $scriptName, and then hover over or type the $scriptName variable to displayits value.
# In C:ps-testMyScript.ps1$scriptName = $MyInvocation.MyCommand.Path# In the Console Pane.MyScript.ps1$scriptNameC:ps-testMyScript.ps1See Also.Related Articles.
![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
March 2023
Categories |