Using conditional breakpoint instances in PowerShell scripts _powershell

Source: Internet
Author: User

Apply to PowerShell 3.0 or higher!

PowerShell Ise only supports line breakpoints: It lets the script run to a specific debug line: You can press F9 to add or remove breakpoints. However, it is necessary to ensure that the script has been saved (the default untitled file may not be possible).

There is also a more advanced approach: a dynamic breakpoint, or a conditional breakpoint, which does not specifically qualify a row, but is limited to a particular scenario. For example, the following sample script, whenever PowerShell assigns a value to a specified variable, stops at the point of interruption:

Copy Code code as follows:

$BP = set-psbreakpoint-variable A-mode write-script $psise. Currentfile.fullpath

$a = 1
$a

$a

$a = 200
$a

Remove-psbreakpoint-breakpoint $BP

You can also bind more advanced and complex conditions, such as if you want to hit a breakpoint when an integer greater than 100 is assigned to $a:

Copy Code code as follows:

$Condition = {if ($a-is [Int]-and $a-gt) {break}}
$BP = set-psbreakpoint-variable A-mode write-script $psise. Currentfile.fullpath-action $Condition

$a = 1
$a

$a

$a = 200
$a

Remove-psbreakpoint-breakpoint $BP

Lai Non-moss NOTE: Conditional breakpoint This technique is quite unpopular, but it is very convenient when it is needed, for example, I assign a value to variable a in a slightly larger script file, but I do not know if I have assigned a wrong value, I want to find this line of code. If there is no conditional breakpoint, I may have to search the keyword for all variables a assignment statement, and hit the breakpoint, the project is vast, and if the use of conditional breakpoints, Amitabha!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.