Datasheet

32 CHAPTER 1
USING POWERSHELL WITH ACTIVE DIRECTORY
TABLE 1.7 Parameter Properties
Property Name Purpose
Mandatory
A Boolean property that determines whether the param-
eter is mandatory in order for the script to run.
Position
An integer property that identifi es the position in which a
positional parameter should appear.
ValueFromPipeline
A Boolean property that indicates whether this parameter
can accept an object that is pipelined in from another
script or cmdlet.
ValueFromPipelineByPropertyName
A Boolean property that indicates whether this param-
eter can accept data being pipelined in from another
command. The diff erence between this and the
ValueFromPipeline
property is that this property
specifi es that the parameter accepts a single parameter
from the pipelined command. The
ValueFromPipeline
property applies to an entire object. If this property is
used, the parameter that is accepted from the pipelined
command is the parameter that uses the same name
defi ned in this command.
HelpMessage
A String property that allows you to specify a help mes-
sage for a mandatory parameter. If the user runs the script
and doesn’t include the mandatory parameter, this help
message is displayed.
Use Functions in a Script
Functions give you the ability to take a block of script code and assign a name to
it. ere are quite a few bene ts to using functions inside your scripts. First, func-
tions allow you to organize scripts into executable chunks, which makes the scripts
easier to edit and troubleshoot. By isolating a block of code into a function, you can
make sure that particular block of code works apart from the rest of your script code.
Another bene t of functions is that they sandbox the scope of your scripts. A variable
that is declared and used inside a function is only valid to that function. And  nally,
functions let you write a block of code once and call it as many times as you like,
making your script code more reusable. Functions are very helpful, so you’ll notice
that throughout this book, we make generous use of them in our sample scripts.
Define Functions
To declare a block of code as a function, you can place the code between opening
and closing curly brackets and pre x it with the following declaration:
Function <FunctionName> (<Parameters>)
c01.indd 32c01.indd 32 5/12/2011 1:07:51 PM5/12/2011 1:07:51 PM