The Set-PSBreakPoint cmdlet units a breakpoint in a script. If you end up troubleshooting a script it could possibly be useful to know what’s occurring in a particualar step or workflow. On this weblog bost I gives you an summary and the fundamentals you may construct on to troubleshoot and examine your script. Let’s soar in.
Beginning Level
The start line for this subject is this straightforward PowerShell script.
for ($i = 100; $i -ge 0; $i -= 3) { Write-Host $i }
Quick clarification: $i is about to 100. The script counts down 3 from 100 till $i is larger or eqal 0.
The script is saved as a ps1 file in C:Temp.
Investigating Traces
Now Set-PSBreakPoint comes into play.
Our subsequent goal is to search out out what worth $i returns in Line 2. It ought to return 100. Let’s test it out with Set-PSBreakPoint and the Line parameter.
Set-PSBreakpoint -Script 'C:Tempfor_5.ps1' -Line 2
After setting the breakpoint, we have to begin the script to get our Breakpoint into motion.
'C:Tempfor_5.ps1'
Wow, there’s a hit … and the hit is highlighted by PowerShell ISE … what a pleasant display!
Okay, now let’s get again to our major goal. What’s the worth of $i? Yeah, it’s 100.
To begin from the scratch within the subsequent half, I cease the debugger by urgent SHIFT + F5 after which I take away all PSBreakPoints.
Get-PSBreakpoint | Take away-PSBreakpoint
Investigating Variables
Now we’ll go one additional. Let’s suppose we need to know what $i returns when $i is lower than 50. As a way to do that, now we have to outline the Variable and the Motion parameter.
Set-PSBreakpoint -Script 'C:Tempfor_5.ps1' -Variable i -Motion { if ($i -lt 50) { break } }
Good. I’ve to clarify that. The variable parameter is about to i, which truly means $i. The Motion parameter accommodates an if assertion. The if assertion is about to $i -lt (lower than) 50 and the break assertion stops the execution of the script when the situation (lt 50) is met.
Let’s give it a attempt!
'C:Tempfor_5.ps1'
If the situation is met $i is 49, not 50. Why? Keep in mind the script counts down by 3.
By the way in which, Set-PSBreakpoint may set breakpoints on instructions and features.
Extra right here: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/set-psbreakpoint?view=powershell-6