10 Building a basic function and script module

 

Remember the tool we made back in chapter 8? Well, go ahead and fire up Visual Studio Code (VS Code) and open that .ps1 file. In this chapter, we’ll take the tool you designed in chapter 8 and turn that into a reusable tool for others to use. 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 been a part of PowerShell since day one, and they’re one of the many types of commands that PowerShell understands (some of the others being cmdlets, applications, etc.). 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 already written about the need to have tightly scoped functions—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.1.1 Designing the input parameters

10.1.2 Writing the code

10.1.3 Designing the output

10.2 Creating a script module

10.3 Prereq check

10.4 Running the command

10.5 Your turn

10.5.1 Start here

10.5.2 Your task

10.5.3 Our take

Summary