Function Parameters in Windows Powershell

Source: Internet
Author: User

In my previous articles on user-defined Windows PowerShell, I have already said that one of the biggest features of PowerShell is the high scalability of function usage. In this article, we will take a closer look at professional functions: Product Quality functions.

What is the difference? Product Quality functions work hard to test input and fix errors when providing information output. When you use a function for a product, you usually want to know if it is interrupted-and you must also want to know why. You need to design parameters and handle errors in other languages. Fortunately, Windows PowerShell has many similar built-in functions.

PowerShell Parameters

When talking about Windows PowerShell functions, we need to consider three things: input, output, and error. This article focuses on input, also known as parameters. PowerShell has many Parameter options and can be used in one of the following three ways:

Location parameters

PowerShell can create a numeric array to pass the $ args variable to the function. Each value passed to the function is added to this array starting from 0. For example:

function foo{Write-Host $args[0] $args[1]}foo "This is parameter 1" "This is parameter 2"

Name Parameter

The parameters entered by PowerShell can also be named, which means they can be passed by name and the values are placed in the corresponding variables. For example, when this function is called, the parameters are reversed, but the values are returned correctly ):

Example (notice the parameters are reversed when the function is called,but the values are returned correctly):function foo{Param($param1,$param2)Write-Host $param1 $param2}foo -param2 "This is parameter 2" -param1 "This isparameter 1"

Splatting Parameters

This is perhaps the most common method in PowerShell parameter passing. It contains creating an array or hash table as the parameter group passed to the function. This allows you to dynamically create parameters for the entire script, and then call the function after you have prepared them. For example:

function foo{Param($param1,$param2)Write-Host $param1 $param2}Create Hash table$blah = @{"Param1"="This is parameter 1";"Param2"="This is parameter 2"}# Pass hash table to functionfoo @Blah

PowerShell parameter attributes

Mandatory-this property is default in PowerShell Parameter options, but if you know the parameter type you need, you can use this property to force users to pass parameters of this type. If they do not, PowerShell reports errors to them and forces them to provide such values so that functions can run properly. For example:

function foo{Param([Parameter(Mandatory=$True)]$param1)Write-Host $param1}

ParameterSetName -- we often need to pass a set of parameters together, which is usually interrupted due to exceptions ). For example, you have a function to get an Active Directory object. If it is a user or a computer, you need to know the account:

function Get-ADObject{Param([Parameter(Mandatory=$True,ParameterSetName="User")]$User,[Parameter(Mandatory=$True,ParameterSetName="Computer")]$Computer)$PScmdlet.ParameterSetName}Get-ADObject --# This will throw an error because noparameters passedGet-ADObject –user "joe" # Will return 'User'Get-ADObject –Computer "joe" # Will return 'Computer'Get-ADObject –User "joe" –Computer "joe" # Will returnan error

ValueFromPipeline -- this attribute tells the function that a specific parameter value can be passed through a pipeline. For example:

function Get-ADUserObject{Param([Parameter(ValueFromPipeline=$true)]$User,)Process{$User}}}$ListofUsers | Get-ADUserObject

ValueFromPipelineByPropertyName -- this attribute seems to be a bit similar to ValueFromPipeline, but it does not use "type". It uses the property name of the incoming object. For example, if you have a property of a user object named UserName.

function Get-ADUserObject{Param([Parameter(ValueFromPipelineByPropertyName=$true)]$Username,)Process{$UserName}}$ListofUserObjects | Get-ADUserObject

HelpMessage -- this allows you to add a help message to the user. If they do not specify the mandatory attribute to call your function, you can explain to them that you need to enter the User name:

function Get-ADComputerObject{Param([Parameter(Mandatory=$True,HelpMessage="Enter computer name.")]$ComputerName,)$ComputerName}

The above information should help you write some product quality functions, but remember that this is only the tip of the iceberg.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.