powershell-Custom Function (iii)-Mandatory parameter: Mandatory

Source: Internet
Author: User

After the first two articles, you should be able to write a simple function. But with the depth of use, you will soon feel overwhelmed.

Because the basic function is to feel too low, with the official or other people write out of the comparison ... The function of weak chickens is not high.

So in order to strengthen our function, let the force lattice up. Start with this article to explain the advanced features.

Goal:

1. Enter a name and age, directly in the show how old this year.

2. Enforce the use of the name parameter.

Example:

Function test-function {Param ([parameter (mandatory= $true)] $Name, $Age = "18 ") Write-host" $Name $Age year old. "}


Description

Compared to the previous article, we added some keywords "[parameter (mandatory= $true)]" in front of the name parameter, including some settings for parameters, which are written in "parameter ()".

In this article, we set the mandatory= $true to indicate that the name parameter must be used when using this function. Because from our point of view, if you do not use the name parameter, it will be displayed as "(blank) How old this year" is obviously inappropriate.

Operation Result:

650) this.width=650; "title=" image "style=" border-right-width:0px;border-bottom-width:0px;border-top-width:0px; " Border= "0" alt= "image" Src= "Http://s3.51cto.com/wyfs02/M00/92/ED/wKioL1kFguSSo4qHAADjA1uHjiI266.png" width= "361" height= "273"/>

As you can see, I ran test-function without specifying any parameters, but PowerShell prompted me to provide a value for the name parameter. This function runs normally until I provide the value of name.

In this way, we will be able to do what we require to specify a person name. Of course, if you've been honest with the name in the beginning, you can run it smoothly.

This article from "Puma pot" blog, declined reprint!

powershell-Custom Function (iii)-Mandatory parameter: Mandatory

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.