The parameter attribute

With a function keyword, the name, and a script block, you are well on your way to creating great reusable code. The next step to improve code quality is to introduce proper parameters.

The parameter attribute decorates each one of your parameters, and allows you to set several options. The most common one is the declaration of a mandatory parameter, as seen in the next code sample:

# The basic parameter attribute
param
(
[Parameter()]
$YourParameter
)

# A mandatory parameter
param
(
[Parameter(Mandatory)]
$YourParameter
)

# A help message for mandatory parameters
param
(
[Parameter(
Mandatory,
HelpMessage = 'This is visible when needed'
)]
$YourParameter
)

# Hidden Parameters
function fun
{
param
(
[Parameter(DontShow)]
$CatchMeIfYouCan
)

$CatchMeIfYouCan
}

# VSCode is too intelligent, ISE will not show IntelliSense
# Tab expansion not possible, Parameter can still be used
fun -CatchMeIfYouCan 'value'

An empty parameter attribute is actually all that it takes to enable the common parameters for a function. Inside the parameter attribute, you can add comma-separated settings. Boolean values, such as Mandatory, may be added without an assignment.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.233.41