Chapter 17. Smashing Those Bugs

In This Chapter

  • Keeping bugs in check

  • Examining bugs under a microscope

  • Using the debugger for effective eradication

One of the most frustrating things about writing any kind of code, such as a Windows PowerShell script, is that inevitably you'll run into a bug. A bug occurs when a piece of code does something that it's not supposed to do or acts a way that it wasn't designed to act. I'm not talking about bugs in Windows PowerShell itself, but bugs in Windows PowerShell scripts that you or someone else wrote. The nastiest kind of bug is one that's hard to reproduce. I like to call this type the sneaky bug (though you might refer to it as your in-laws). Whatever it is, when you do find a bug, you need to exterminate it quickly; otherwise, that bug will be staring right into your face someday at the most inopportune moment.

In this chapter, you make use of the new debugging features in Windows PowerShell 2 that'll make it much easier to track down errors in your code so you won't have to keep hitting your head on the wall in frustration the next time your script doesn't work as you expect.

Finding Out Where the Bugs Come From

Software bugs are never intentional. Instead, they usually occur when the script or program enters a state that it wasn't designed for. When you find a bug in a script, you'll usually find that it's caused by one of these situations:

  • Unexpected input

  • Incorrect logic

  • System error

Guarding against unexpected input

Unexpected input occurs when ...

Get Windows PowerShell™ 2 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.