Debugging PowerShell script in Visual Studio Code Part 1
Debugging PowerShell script in Visual Studio Code Part 1
Summary: Here’s a look at the many features of the PowerShell debugger for Visual Studio Code.
In previous blog posts, we covered how to get started with PowerShell development in Visual Studio Code and the editing features of Visual Studio Code and the PowerShell extension . If you don’t already have Visual Studio Code configured with the PowerShell extension, read those blog posts to get caught up.
In the first of this two-part series, we will cover the many features of the PowerShell debugger for Visual Studio Code. These features are provided by the PowerShell extension, or, more accurately, by the PowerShell Editor Services module which comes with the PowerShell extension.
PowerShell Editor Services runs in a separate process and supplies both language and debugging services to Visual Studio Code via a JSON remote procedure call (RPC) protocol that’s defined by Visual Studio Code. One advantage of this approach is that a crash of the PowerShell Editor Services process doesn’t cause Visual Studio Code to crash. And, with the latest version of the PowerShell extension, you can simply restart the current PowerShell session without restarting Visual Studio Code to get going again.First look at the PowerShell Debugger in Visual Studio Code
Press Ctrl+Shift+P (Cmd+Shift+P on Mac) to open the PowerShell extension’s Examples folder, type PowerShell open examples , and then press Enter. After the Examples folder has loaded, open the DebugTest.ps1 file, and set a breakpoint on the line that has the Start-Sleep command. To set the breakpoint, either click in the left editor margin or press F9 to toggle the breakpoint on and off for the current line.
To open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D. In the LaunchConfiguration dropdown (shown in the following screenshot), select the PowerShell Launch (current file) configuration. Like the PowerShell integrated scripting environment (ISE), this configuration will execute the file that’s in the active editor window under the debugger when debugging is started.
Let’s start a debug session. First, make sure the DebugTest.ps1 file’s editor window is still the active window, and then press F5 or click the green Start Debugging button to the left of the LaunchConfiguration dropdown (shown in the previous screenshot).
After the debugger starts, you will see the Debug actions pane (shown in the following screenshot), and the debugger should pause at the breakpoint that you set.
The Debug actions pane provides buttons for:Continue / Pause F5 Step Over F10 Step Into F11 Step Out Shift + F11 Restart Ctrl + Shift + F5 Stop Shift + F5
Now, let’s look at the Debug view features that are available during a debug session.
The VARIABLES section of the Debug view allows easy inspection of variable values. The Auto group weeds out the PowerShell automatic variables and leaves just the variables you’ve defined and are likely interested in seeing. However, if the variable you are looking for isn’t listed in Auto , you can look for it in the Local , Script , or Global groups.
The WATCH section allows you to specify a variable or expression whose value should always be displayed.
The CALL STACK section displays the call stack, and you can also select a different frame in the call stack to examine calling functions, scripts, and the variables that are defined in those scopes.
The BREAKPOINTS section provides a central UI to manage, that is, create, disable, enable, and delete breakpoints that you may have defined over many different script files.
You can also see from the previous screenshot that you get hover tips when you hold the cursor over a variable. Hover tips can show simple values, like numbers and strings, or complex objects as shown in the following screenshot:
The VARIABLES section allows you to inspect variable values, including complex variables such as those shown in the following screenshot:
For primitive variable types, the value is displayed directly, typically as numbers, strings, and Booleans. For non-primitive variables, the type information is displayed. If the type is a collection or an array, the number of elements is displayed as well.
You can do more than just inspect variable values. To change those values, double-click the value that you want to change, enter a new value, and click outside the edit box to complete the operation.
You can enter arbitrary expressions when setting a variable’s value, for example, $itemCount+10 or $null or $true . Just remember, the expression has to be valid PowerShell syntax.
The WATCH section allows you to add a watch for any variable or expression. Simply click the + button (highlighted in the following screenshot), and type the variable name or a PowerShell expression:
This values will always be evaluated, if possible. Keep in mind that the variables entered as a watch may not be available in all scopes.BREAKPOINTS
Besides setting line breakpoints, the PowerShell debugger allows you to set function breakpoints, conditional breakpoints, and tracepoints.Function breakpoints
Function breakpoints are effectively the same as a command breakpoint that you can set by using Set-PSBreakpoint with the -Command parameter. You can set a function breakpoint to break into the debugger not only on a particular function invocation but also on an alias, a built-in command, or application invocation.
To set a function breakpoint, hover over the BREAKPOINTS section title bar, click the + button, type Write-Output , and then press Enter as shown in the following screenshot.
Remove the line breakpoint that we set earlier on the line that executes the Start-Sleep command.Press F5 to start debugging the DebugTest.ps1 script, and you will see the debugger stop everywhere Write-Output is called. You can tell when the debugger is stopped on a function breakpoint by looking at the CALL STACK section of the Debug view. It will indicate that it is paused on a function breakpoint. The Debug Console will also indicate that a breakpoint has paused the debugger as shown in the following screenshot. If the Debug Console is not visible, select Debug Console from the View menu, or
本文系统（windows）相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术
本文标题：Debugging PowerShell script in Visual Studio Code Part 1