Chapter 23. Debugging techniques

 

Anytime you’ve typed more than two letters on your keyboard, you have created an opportunity for mistakes to creep in. In the programming world, those mistakes are called bugs. Although using PowerShell isn’t necessarily programming, we PowerShell jockeys use the word bug, too.

Fun story: The word bug, as applied to computers behaving incorrectly, actually came from a real insect (a moth) that got trapped inside Harvard University’s Mark II Aiken Relay Calculator. The moth got stuck in one of the computer’s relays, causing the computer to generate incorrect results. The whole story is told at http://www.jamesshuggins.com/h/tek1/first_computer_bug.htm.

23.1. An easy guide to eliminating bugs

There are two broad categories of bugs in the PowerShell world (actually, this applies to software in general, but we’ll stick with PowerShell). The first category is syntax errors, and the second is logic errors.

23.1.1. Syntax errors

Syntax errors are by far the easiest to deal with, and we won’t spend much time on them in this chapter. A syntax error simply means you typed something wrong. It might be a command name that you misspelled (Gte-Content instead of Get-Content, for example), or it might be that you got the actual syntax of the command wrong (forgetting the hyphen between the verb and noun of a cmdlet name, or using a colon instead of a space to separate a parameter’s name and value). Whatever the cause, correcting your typing will solve the problem.

23.2. Identifying your expectations

23.3. Adding trace code

23.4. Working with breakpoints

23.5. Common points of confusion

23.6. Lab