{"id":11774,"date":"2016-04-20T06:00:09","date_gmt":"2016-04-20T13:00:09","guid":{"rendered":"https:\/\/www.sapien.com\/blog\/?p=11774"},"modified":"2016-04-29T19:22:38","modified_gmt":"2016-04-30T02:22:38","slug":"setting-conditional-breakpoints","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2016\/04\/20\/setting-conditional-breakpoints\/","title":{"rendered":"Setting Conditional Breakpoints"},"content":{"rendered":"<p><strong>Applies to PowerShell Studio 2016, PrimalScript 2016, and later.<\/strong><\/p>\n<hr \/>\n<p>I write lots of scripts \u2014 for production, for research, for testing, and for demonstrations. And, inevitably, I hit a bug. I&#8217;m actually relieved when I do, because no code is perfect and I&#8217;d rather find the bugs before my users do. In fact, I make sure the content in my test database is full of oddities so it&#8217;s as close to the real world as possible.<\/p>\n<p>When you are debugging with a large test sample, like a database or directory, the default line breakpoints are not sufficient. I can hit a line numerous times before I get to the condition that breaks it. And, I don&#8217;t want to spend my time repeatedly banging on the F10 (step over) and F11 (step into) keys until I hit the offending condition.<\/p>\n<p>In PowerShell Studio 2016 and PrimalScript 2016, you can create and edit line, function, and variable breakpoints, and beyond that, you can set conditions for breaking. For example, I don&#8217;t want to break on line 32 every time it runs. I want to break when the $ID variable has a value of 9999 or only when the Set-Widget function runs and $ID is 0.<\/p>\n<h2>Good old line breakpoints<\/h2>\n<p>Line breakpoints break just before the line of code with the breakpoint runs. Because most debuggers support line breakpoints, we&#8217;re very accustomed to using them. But, they might not be most efficient, so before you use a line breakpoint, consider whether a function or variable breakpoint might be a better choice.<\/p>\n<p>One of the nice things about line breakpoints is that they&#8217;re easy to see. You can see the big red dot beside the breakpoint on line 24 in this image. But if you want to break when the Set-Maximum function runs \u2014 not just when it&#8217;s called on line 24, consider a function breakpoint.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image002.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11776\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image002.png\" alt=\"function breakpoint\" width=\"338\" height=\"185\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image002.png 338w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image002-300x164.png 300w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><\/a><\/p>\n<h2>Use Function Breakpoints<\/h2>\n<p>A function breakpoint breaks right before the first line of code in the function runs. It actually breaks on the first curly brace in the function script block.<\/p>\n<p>If the function has BEGIN, PROCESS, and END blocks, a function breakpoint breaks on the first curly brace of the BEGIN block, then on the first curly brace of the PROCESS block, and again on the first curly brace of the END block.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image004.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11777\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image004.png\" alt=\"a function breakpoint breaks on the first curly brace of the BEGIN block, then on the first curly brace of the PROCESS block, and again on the first curly brace of the END block.\" width=\"446\" height=\"248\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image004.png 446w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image004-300x167.png 300w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/a><\/p>\n<p>The breakpoint does not break on the function declaration \u2014 when PowerShell reads the function into memory. It breaks only on the function call \u2014 where the function actually runs. So, if you have defined a function in a script, but the function doesn&#8217;t run, the function breakpoint is not triggered.<\/p>\n<p>Also, by default, a function breakpoint breaks every time the function runs, which might be handy, depending on your use case.<br \/>\n&nbsp;<br \/>\n<strong>To set a function breakpoint in PowerShell Studio:<\/strong><\/p>\n<ol>\n<li>Click Home and, in the Run group, click <strong>Breakpoints<\/strong> \/ <strong>Set Function Breakpoint<\/strong> or <strong>Edit Breakpoints<\/strong>.<br \/>\n<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image006.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11778\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image006.png\" alt=\"Click Home and, in the Run group, click Breakpoints \/ Set Function Breakpoint or Edit Breakpoints.\" width=\"244\" height=\"157\" \/><\/a>\n<\/li>\n<li>Select a function from the Function drop-down menu. You can also specify a file if you&#8217;re debugging with multiple files in a module or project.<br \/>\n<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image008.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11779 size-full\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image008.png\" alt=\"specify a file if you're debugging with multiple files in a module or project.\" width=\"365\" height=\"101\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image008.png 365w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image008-300x83.png 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/a><\/p>\n<p>You can&#8217;t see the function breakpoint \u2014 there&#8217;s no big red dot in your script \u2014 but it&#8217;s effective.<br \/>\n<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image010.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11780 size-full\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image010.png\" alt=\"You can't see the function breakpoint -- there's no big red dot in your script -- but it's effective.\" width=\"377\" height=\"277\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image010.png 377w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image010-300x220.png 300w\" sizes=\"auto, (max-width: 377px) 100vw, 377px\" \/><\/a><\/p>\n<p>And, the <strong>Edit Breakpoints<\/strong> dialog box display shows that it&#8217;s created and enabled.<br \/>\n<a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image012.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-11781 size-full\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image012.png\" alt=\"the Edit Breakpoints dialog box display shows that it's created and enabled.\" width=\"470\" height=\"143\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image012.png 470w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image012-300x91.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/p>\n<p>TIP : I always use the Edit Breakpoints dialog to create, enable, disable, and delete my breakpoints. It displays all of the breakpoints in the session and reminds when that I create a breakpoint twice, instead of changing a breakpoint. To create a function breakpoint in Edit Breakpoints, click the &#8220;cube+&#8221; icon in the upper left corner.<\/li>\n<\/ol>\n<h2>Use Variable Breakpoints<\/h2>\n<p>Variable breakpoints are my favorites. They break when the variable value is used (&#8220;read&#8221;), is changed (&#8220;write&#8221;), or either (&#8220;ReadWrite&#8221;).<br \/>\n&nbsp;<br \/>\n<strong>To set a variable breakpoint:<\/strong><\/li>\n<ol>\n<li>Click Home and, in the Run group, click <strong>Breakpoints<\/strong> \/ <strong>Set Variable Breakpoint<\/strong>.<br \/>\n&nbsp;<br \/>\n-or-<br \/>\n&nbsp;<br \/>\nIn the Edit Breakpoints dialog, in the upper left corner, click the &#8220;square+&#8221; icon.<\/li>\n<p>&nbsp;<\/p>\n<li>Select a variable from the Variable drop-down menu.\n<p>You can also type the variable name. Technically, you&#8217;re supposed to enter only the variable name without the &#8216;$&#8217;, but PowerShell Studio is wonderful, so if you forget and type the &#8216;$&#8217;, PowerShell Studio ignores it.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image014.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11782\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image014.png\" alt=\"Create a variable breakpoint\" width=\"469\" height=\"218\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image014.png 469w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image014-300x139.png 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/a><\/li>\n<li>From the Mode box, select <strong>Read<\/strong> (break whenever the variable value is read), <strong>Write<\/strong> (break when the variable value changes), or <strong>ReadWrite<\/strong> (both).<\/li>\n<p>Like a function breakpoint, you won&#8217;t see a big red dot in your script to indicate a variable breakpoint, but you can see it in the Edit Breakpoints dialog box. (See why I like that dialog so much?)<\/p>\n<p>For example, in this script, a write-mode variable breakpoint on &#8216;n&#8217; is triggered on line 26, when the value of $n changes to the next value in the $numbers array. But, it is not triggered on line 28, when $n is read, but not changed. If I need a different behavior, I can change the mode of the breakpoint to Read or ReadWrite.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image016.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11783\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image016.png\" alt=\"a write-mode variable breakpoint on 'n' is triggered on line 26, when the value of $n changes to the next value in the $numbers array.\" width=\"352\" height=\"208\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image016.png 352w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image016-300x177.png 300w\" sizes=\"auto, (max-width: 352px) 100vw, 352px\" \/><\/a>\n<\/ol>\n<h2>Create conditional breakpoints<\/h2>\n<p>Variable and function breakpoints are very valuable, but conditional breakpoints are the extreme time-saver \u2014 an uber-breakpoint.<\/p>\n<p>To set a conditional breakpoint:<\/p>\n<ul>\n<li>When creating or editing a breakpoint, in the <strong>Action<\/strong> box, enter a script block.<\/li>\n<li>The Action value <strong>replaces<\/strong> the break in the breakpoint. That is, when the breakpoint event occurs and an Action is defined, Windows PowerShell runs the Action script block <strong>instead of<\/strong> breaking (not in addition to breaking). If you want it to break, use the Break keyword (see <a href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkID=113213\">about_Break<\/a>).<\/li>\n<\/ul>\n<p>The value in the Action box is a script block that runs when the breakpoint is triggered. <\/p>\n<p>The Action script block should be enclosed in curly braces, but PowerShell Studio is wonderful, so if you forget the curly braces, it adds them automatically. I like to type the curly braces, but it&#8217;s not necessary.<\/p>\n<p>For example, this variable breakpoint breaks when the value of $n is changed to 4.<\/p>\n<pre lang=\"PowerShell\">{ if ($n -eq 4) { break } }<\/pre>\n<p>In the Action script block, the &#8216;n&#8217; variable is referred to as $n, just like it is in my script. I use the Break keyword in my script block to tell Windows PowerShell to break into the debugger when the condition is met.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image018.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11784\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image018.png\" alt=\"use the Break keyword in my script block to tell Windows PowerShell to break into the debugger when the condition is met.\" width=\"470\" height=\"93\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image018.png 470w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image018-300x59.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/p>\n<p>When an Action is set, the variable breakpoint is still triggered every time the value of $n changes. But, the script does not break into the debugger. Instead, it runs the Action script block. If $n is not 4, the &#8216;{break}&#8217; in the Action doesn&#8217;t occur and the script continues.<\/p>\n<p>But when the variable breakpoint is triggered on a change in $n, and the value of $n is 4, the script breaks into the debugger.<\/p>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image020.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11785\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image020.png\" alt=\"when the variable breakpoint is triggered on a change in $n, and the value of $n is 4, the script breaks into the debugger.\" width=\"469\" height=\"310\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image020.png 469w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image020-300x198.png 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/a><\/p>\n<p>You can set conditional function breakpoints, too. For example, this function breakpoint breaks into the debuggers only when the Get-DeviceID function is called with a parameter value of &#8216;localhost&#8217;. Otherwise, it records the value of the ComputerName in a text file.<\/p>\n<pre lang=\"PowerShell\">if ($ComputerName -eq 'localhost') { break } else { $ComputerName | Out-File .\\Test.txt }<\/pre>\n<p><a href=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image022.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-11786\" src=\"https:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image022.png\" alt=\"this function breakpoint breaks into the debuggers only when the Get-DeviceID function is called with a parameter value of 'localhost'.\" width=\"470\" height=\"260\" srcset=\"https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image022.png 470w, https:\/\/dev.sapien.com\/blog\/wp-content\/uploads\/2016\/04\/image022-300x166.png 300w\" sizes=\"auto, (max-width: 470px) 100vw, 470px\" \/><\/a><\/p>\n<p>Conditional breakpoints are so powerful that, after a bit of experience, you&#8217;ll use them all the time. Just remember to include the Break keyword if you want to break into the debugger.<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. and a Windows PowerShell MVP. You can reach her at <a href=\"mailto:juneb@sapien.com\">juneb@sapien.com<\/a> or follow her on Twitter at <a href=\"https:\/\/twitter.com\/juneb_get_help\" target=\"_blank\">@juneb_get_help<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Applies to PowerShell Studio 2016, PrimalScript 2016, and later. I write lots of scripts \u2014 for production, for research, for testing, and for demonstrations. And, inevitably, I hit a bug. I&#8217;m actually relieved when I do, because no code is perfect and I&#8217;d rather find the bugs before my users do. In fact, I make [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[27,283,703,1055,303,25],"tags":[618,1093,1094,282,244,1095],"class_list":["post-11774","post","type-post","status-publish","format-standard","hentry","category-ask-the-experts","category-howto","category-powershell-studio","category-scripting","category-tip","category-windows-powershell","tag-breakpoints","tag-conditional-breakpoints","tag-function-breakpoints","tag-functions","tag-variable","tag-variable-breakpoints"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=11774"}],"version-history":[{"count":36,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11774\/revisions"}],"predecessor-version":[{"id":11911,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/11774\/revisions\/11911"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=11774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=11774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=11774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}