PowerShell Mutex parameter usages

Source: Internet
Author: User
Tags mutex

This article mainly introduces the use of PowerShell mutex parameters, this article gives two code examples to solve the use of mutually exclusive parameters, the need for friends can refer to the

Sometimes PowerShell functions need to be mutually exclusive, allowing users to select only one or two of them.

To create a set of mutually exclusive arguments to the script, you can give them different attribute flags and ensure their uniqueness (assuming that the parameter type is not recognized automatically).

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16-17 function Test-parameterset {[Cmdletbinding (defaultparametersetname= ' number ')] param ([int] [Parameter ( Parametersetname= ' number ', position=0)] $id, [string] [Parameter (parametersetname= ' text ', position=0)] $name) $PSCmdl Et. Parametersetname $PSBoundParameters}

The above function has two parameters,-id and-name. Users can only use one of them and not both. At the same time here The example also teaches how to get the user input parameters.

Multiple mutex parameters can be distinguished by "parametersetname" to distinguish between different parameter settings (also including collection parameters)

In fact, you can also assign multiple parameters to a parameter, a fixed parameter and an optional parameter.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 function Test-parameterset {[Cmdletbinding (defaultparametersetname= ' noncredential ')] param ($id, [Parameter ( Parametersetname= ' localonly ', mandatory= $false)] $LocalAction, [Parameter parametersetname= ' credential ', mandatory = $true)] [Parameter (parametersetname= ' noncredential ', mandatory= $false)] $ComputerName, [Parameter ( Parametersetname= ' credential ', mandatory= $false)] $Credential) $PSCmdlet. Parametersetname $PSBoundParameters if ($PS Boundparameters.containskey (' ComputerName ')) {write-warning ' Remote call! '}}

The Test-parameterset function tells you that when you use "noncredential",-computername is optional, and when the user uses the "-credential" parameter "-computername" becomes a required parameter, if you use the " -localaction "parameter, you will not be able to use-computername and-credential.

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.