未加星标

Debugging PowerShell script in Visual Studio Code Part 1

字体大小 | |
[系统(windows) 所属分类 系统(windows) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

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.


Debugging PowerShell script in Visual Studio Code   Part 1

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.


Debugging PowerShell script in Visual Studio Code   Part 1

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.


Debugging PowerShell script in Visual Studio Code   Part 1

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:


Debugging PowerShell script in Visual Studio Code   Part 1
VARIABLES section

The VARIABLES section allows you to inspect variable values, including complex variables such as those shown in the following screenshot:


Debugging PowerShell script in Visual Studio Code   Part 1

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.


Debugging PowerShell script in Visual Studio Code   Part 1
Watch section

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:


Debugging PowerShell script in Visual Studio Code   Part 1

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.


Debugging PowerShell script in Visual Studio Code   Part 1

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)相关术语:三级网络技术 计算机三级网络技术 网络技术基础 计算机网络技术

分页:12
转载请注明
本文标题:Debugging PowerShell script in Visual Studio Code Part 1
本站链接:http://www.codesec.net/view/532891.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 系统(windows) | 评论(0) | 阅读(20)