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