Chapter 10. Building a basic function and script module

 

In this chapter, you’ll start creating the tool that you designed in chapter 8, using some of the commands that you figured out and tested in the previous chapter. It’s important to understand that this chapter isn’t going to attempt to have you build the entire tool or solve the entire business statement from chapter 8. We’ll take things one step at a time, because it’s the process of toolmaking that we want to demonstrate for you.

10.1. Starting with a basic function

Basic functions have existed in PowerShell since v1, and they’re one of the many types of commands that PowerShell understands (some of the others being cmdlets, applications, workflows, and so on). Functions make a great unit of work for toolmaking, as long as you follow the basic principle of keeping your function tightly scoped and self-contained. We’ve written already about the need to have tightly scoped functions—that is, functions that do just one thing. Self-contained means the function needs to live in its own little world and become a kind of black box. Practically speaking, that means two things:

10.2. Creating a script module

10.3. Prereq check

10.4. Running the command

10.5. Your turn