Chapter 21. Creating your own “cmdlets” and modules

 

At the end of chapter 19, you saw how to make a function (in listing 19.5) that output custom objects to the pipeline. Mastering that kind of output is a key to becoming a PowerShell guru, but there’s also the question of input.

In chapter 19, we passed input to the function by means of a parameter. In this chapter, we’re going to look at some other means of getting input into the function. By combining different input techniques with what you already know about producing output, you’ll find that you can create a tool that behaves almost exactly like a PowerShell cmdlet!

21.1. Turning a reusable tool into a full-fledged cmdlet

As I said, the function in listing 19.5 accepted input primarily through a parameter. In order to make a tool like that more useful, it would be nice if we could pass in multiple pieces of input (the function in listing 19.5 only worked with a single computer name, for example), and pass them in either using a parameter or from the pipeline. That would give us a fully reusable tool that looks and works much like a cmdlet. Ideally, we could even have the shell do some input validation for us, such as marking a parameter as mandatory and automatically prompting the user if it wasn’t provided.

21.2. Functions that work in the pipeline

 
 
 

21.3. Functions that look like cmdlets

 
 

21.4. Bundling functions into modules

 
 
 
 

21.5. Keeping support functions private

 

21.6. Lab

 
 
 
 

21.7. Ideas for on your own

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest