Saturday 27 July 2013

Debugging PowerShell scripts

This week I've been working on a couple of PowerShell scripts to automate a few tasks around deployment of an application and because I didn't know any better, I returned to the good bad old technique of writing out the variable values to see what the problem was. It turns out that it is possible to debug PowerShell scripts without using the ISE, which was crashing for me every time I tried to run it.

Enter the Set-PSBreakpoint cmdlet:


It's worth noting that you can set multiple breakpoints with the same command and as you can probably imagine they can be added to commands, variables or actions, for more details see this:
Get-help Set-PSBreakpoint -examples
In order to remove the all breakpoints, just use:
Get-PSBreakpoint | ForEach-Object {Remove-PSBreakpoint $_}
Or delete a single one
Remove-PSBreakpoint -id 0
Hope this helps, if it doesn't use Powershell ISE, don't waste your time adding write-host or echo statements to output values like I did this week.

No comments:

Post a Comment