The PowerShell function parameter is set to the optional and required method _powershell

Source: Internet
Author: User

This article describes the use of a parameter set in PowerShell custom functions by setting the same argument to optional and mandatory.

OK, small make admits, this topic is a bit boring, but it is still a little interesting, might as well take a look at it.

In PowerShell, we might have the requirement that this parameter is optional if you only need to enter a parameter. But if you have to enter other parameters, then this parameter becomes a must choice. So how does this need to be met? That is the meaning of this article.

Copy Code code as follows:

function Connect-somewhere
{
[Cmdletbinding (defaultparametersetname= ' A ')]
Param
(
[Parameter (Parametersetname= ' A ', mandatory= $false)]
[Parameter (Parametersetname= ' B ', mandatory= $true)]
$ComputerName,
[Parameter (Parametersetname= ' B ', mandatory= $false)]
$Credential
)
$chosen = $PSCmdlet. Parametersetname
"You have chosen $chosen parameter set."
}

Read the above function, is there any doubt? The above PowerShell function defines a set of two parameters, one with a parameter set named A, and the other named B. Parameter set A has only one parameter $computername, and it is optional. and the parameter set B has two parameters $computername and $credential. If we use parameter set a, the input does not need to enter computername This parameter, because it is not required. And if we use the parameter set B, this time we have to fill in the $computername.

And look at the following function call example.

Copy Code code as follows:

Ps> Connect-somewhere
You have chosen A parameter set.
Ps> connect-somewhere-computername Test
You have chosen A parameter set.
Ps> Connect-somewhere-credential User1
Cmdlet Connect-somewhere at command pipeline position 1
Supply values for the following parameters:
computername:nowmandatory!
You have chosen B parameter set.

The first invocation case is to use the default parameter set, in which the default parameter set is the parameter set a, the parameter set a has only one argument $computername, and is optional, so it is OK to enter nothing.

The second invocation case uses a computername parameter, conforms to the condition of parameter set a, and automatically matches the parameter set a.
The third invocation case uses only one credential parameter, which appears in the parameter set B. However, if you use parameter set B, you must fill in the ComputerName parameter, so the error is correct.

On the PowerShell function parameters can be selected and must be selected, this article on the introduction of so many, I hope to help you, thank you!

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.