25 Debugging techniques

 

In the previous chapter, we talked about how to handle bad situations (errors and exceptions) but specifically the bad situations that you were expecting. There’s another type of bad situation that can happen as your scripts become more and more complex. These situations, that we’ve alluded to already, are called bugs. These are the side effects of a long night scripting, or lack of coffee in the morning. In other words, they’re the side effects of us being human. We all make mistakes, and this chapter will be focused on some techniques on how to find and squash the bugs in your scripts.

NOTE

We’ll be diving into some of the features that the PowerShell extension for Visual Studio Code has to offer, so if you need a refresher on how to get set up, be sure to go back to chapter 2 and follow the steps there.

25.1 Output everything

Without diving too deep into Azure Pipelines concepts, the script in listing 25.1 will get details about published artifacts we care about and download them to the Temp drive. If you’ve never heard the term artifact before, it’s basically a file that has been published somewhere other tools can download it. Additionally, you’ll notice some environment variables in the script (prefixed with $env:). This is because the script was written to be run inside of Azure Pipelines where these artifacts exist.

25.2 One line at a time

25.3 Hey, script, stop right there . . . with breakpoints

25.4 Lab