Chapter 4. Simple scripts and functions

 

Depending on how much you’ve worked with PowerShell already, this chapter may be a bit of a refresher. That’s okay, because we’re going to quickly build on what this chapter covers. In fact, you can think of this chapter as your true starting point in tool-making, now that we’ve reviewed some of the foundation topics. This chapter represents the point where tool users usually stop and where true tool maker begins.

4.1. Start with a command

Whenever we set out to build a tool, we usually start at the shell’s command line. Internally, tools run PowerShell commands in a specific sequence, so we’ll start by getting our command working properly. After all, at the command line we have less complication to worry about and fewer moving parts. It’s a lot easier to get the command working there and to then move it into a script than to start in a script where many other things are going on. Start simple and then build complexity!

For this chapter, we’ll start with a simple tool that retrieves some basic operating system information from a remote computer. Here’s the command, along with a sample of its output:

4.2. Turn the command into a script

 

4.3. Parameterize the command

 
 

4.4. Turn the script into a function

 
 
 

4.5. Testing the function

 
 
 
 

4.6. Lab

 
 
 
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