多个强制参数集 [英] Multiple Mandatory Parameter Sets

查看:43
本文介绍了多个强制参数集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个包含多个参数集的函数,其中一些是强制性的,一些是可选的.

I am working on a function that contains multiple parameter sets, with some being mandatory and some optional.

这只是一个例子,但请想象以下情况:

This is just an example, but imagine the following situation:

从 AD 组中添加或删除用户或计算机的功能(出于某种原因,您需要区分用户和计算机).

A function to add or remove either a user or a computer from an AD group (where for some reason you need to distinguish between users and computers).

请记住这只是一个例子.在这种情况下,使用 ValidateSet() 添加/删除单个 [string] 参数会容易得多,但这不是重点.

Please remember this is just an example. In this case it would be far easier to make add/remove a single [string] parameter with a ValidateSet(), but that is beside the point.

所以你有 4 个参数集:

So you have 4 parameter sets:

Param
(
    [Parameter(ParameterSetName = 'Add', Mandatory = $true)][switch] $Add,
    [Parameter(ParameterSetName = 'Remove', Mandatory = $true)][switch] $Remove,

    [Parameter(ParameterSetName = 'User', Mandatory = $true)][switch] $User,
    [Parameter(ParameterSetName = 'Computer', Mandatory = $true)][switch] $Computer
)

现在的问题是你只能使用四个参数之一,而不是(Add or Remove) and (User or Computer)

The problem now is that you can only use one of the four parameters, rather than (Add or Remove) and (User or Computer)

我知道可以为每个参数使用多个参数集,但我看不到强制它具有两个始终强制的参数集的方法.实际上,您必须始终指定添加或删除以及用户或计算机.

I know it's possible to use multiple parameter sets per parameter, but I can't see a way of forcing it to have two parameters sets which are always mandatory. Effectively you must always specify either Add or Remove and also either User or Computer.

如何做到这一点?

推荐答案

您需要为这些不同的组合定义多个参数集.试试这个:

You need multiple parameter sets defined for these different combinations. Try this:

Function TestParamSet {
    [CmdletBinding()]
    Param
    (
        [Parameter(ParameterSetName = 'AddUser', Mandatory = $true)]
        [Parameter(ParameterSetName = 'AddComputer', Mandatory = $true)]
        [switch] $Add,

        [Parameter(ParameterSetName = 'RemoveUser', Mandatory = $true)]
        [Parameter(ParameterSetName = 'RemoveComputer', Mandatory = $true)]
        [switch] $Remove,

        [Parameter(ParameterSetName = 'AddUser', Mandatory = $true)]
        [Parameter(ParameterSetName = 'RemoveUser', Mandatory = $true)]
        [switch] $User,

        [Parameter(ParameterSetName = 'AddComputer', Mandatory = $true)]
        [Parameter(ParameterSetName = 'RemoveComputer', Mandatory = $true)]
        [switch] $Computer
    )

    Process {
        #Do Nothing
    }
}

而且,当您获得有关此功能的帮助时,您会看到以下内容:

And, this is what you see when you get help on this function:

PS C:\> Get-Help TestParamSet 

NAME
    TestParamSet

SYNTAX
    TestParamSet -Add -Computer  [<CommonParameters>]

    TestParamSet -Add -User  [<CommonParameters>]

    TestParamSet -Remove -Computer  [<CommonParameters>]

    TestParamSet -Remove -User  [<CommonParameters>]


ALIASES
    None


REMARKS
    None

这篇关于多个强制参数集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆