TOPIC
about_functions_advanced
SHORT DESCRIPTION
Introduces advanced Functions that act similar to cmdlets.
LONG DESCRIPTION
Advanced Functions allow you to write Functions that can perform operations
that are similar to the operations you can perform with cmdlets. Advanced
Functions are helpful when you want to quickly write a Function without
having to write a compiled cmdlet using a Microsoft .NET Framework
language. These Functions are also helpful when you want to restrict the
Functionality of a compiled cmdlet or when you want to write a Function
that is similar to a compiled cmdlet.
There is a difference between authoring a compiled cmdlet and an advanced
Function. Compiled cmdlets are .NET Framework classes that must be written
in a .NET Framework language such as C#. In contrast, advanced Functions
are written in the Windows PowerShell script language in the same way that
other Functions or script blocks are written.
Advanced Functions use the CmdletBinding attribute to identify them as
Functions that act similar to cmdlets. The CmdletBinding attribute is
similar to the Cmdlet attribute that is used in compiled cmdlet classes to
identify the class as a cmdlet. For more information about this attribute,
see about_functions_CmdletBindingAttribute.
The following example shows a Function that accepts a name and then prints
a greeting using the supplied name. Also notice that this Function defines
a name that includes a verb (Send) and noun (Greeting) pair similar to the
verb-noun pair of a compiled cmdlet. However, Functions are not required
to have a verb-noun name.
Function Send-Greeting
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string] $Name
)
Process
{
Write-Host (“Hello ” + $Name + “!”)
}
}
The parameters of the Function are declared by using the Parameter
attribute. This attribute can be used alone, or it can be combined with
the Alias attribute or with several other parameter validation attributes.
For more information about how to declare parameters (including dynamic
parameters that are added at runtime), see
about_functions_advanced_parameters.
The actual work of the previous Function is performed in the Process
block, which is equivalent to the ProcessingRecord method that is used by
compiled cmdlets to process the data that is passed to the cmdlet. This
block, along with the Begin and End blocks, is described in the
about_functions_advanced_methods topic.
Advanced Functions differ from compiled cmdlets in the following ways:
– Advanced Function parameter binding does not throw an exception when
an array of strings is bound to a Boolean parameter.
– The ValidateSet attribute and the ValidatePattern attribute cannot
pass named parameters.
– Advanced Functions cannot be used in transactions.
SEE ALSO
about_functions_advanced_CmdletBindingAttribute
about_functions_advanced_methods
about_functions_advanced_parameters
Windows PowerShell Cmdlets (http://go.microsoft.com/fwlink/?LinkID=135279)