1

我有一个具有以下参数的函数:

param (
    [Parameter  (Mandatory = $false,
                ParameterSetName = "SendMail",
                HelpMessage = "Call this parameter to send information mail ONLY.")]
    [switch]$SendMail,

    [Parameter  (Mandatory = $false,
                Position = 0,
                ParameterSetName = "UseRegion",
                HelpMessage = "Call this parameter to use the function with Regions.")]
    [Parameter  (Mandatory = $false,
                ParameterSetName = "UseCountry")]
    [switch]$UseRegions,

    [Parameter  (Mandatory = $false,
                ParameterSetName = "SendMail")]
    [Parameter  (Mandatory = $true,
                ParameterSetName = "UseRegion",
                HelpMessage = "Define the region to apply the function.")]
    [ValidateSet("AMER","APAC","EMEA")]
    [string]$Region,

    [Parameter  (Mandatory = $false,
                ParameterSetName = "SendMail")]
    [Parameter  (Mandatory = $false,
                ParameterSetName = "UseRegion")]
    [Parameter  (Mandatory = $true,
                HelpMessage = "Define the country to apply the function.",
                ParameterSetName = "UseCountry")]
    [ValidateNotNullOrEmpty()]
    [string]$Country,
    
    [Parameter  (Mandatory = $false,
                HelpMessage = "Define months for a machine to be outdated. Default value is 3.")]
    [ValidateRange(1, [int]::MaxValue)]
    [int]$Months = 3,

    [Parameter  (Mandatory = $false,
                HelpMessage = "Define days for a machine to be on the ""Disabled"" OU before permanent deletion. Minimum and default value is 7.")]
    [ValidateRange([int] 7, [int] 30)]
    [int]$DaysToDeletion = 7,

    [Parameter  (Mandatory = $false,
                HelpMessage = "Define operation to exclude from the rule. I.E.: Operations that does not connect to the VPN.")]
    [string]$Exclude
)

我正在尝试完成以下操作:

当我调用 -SendMail 时,NO 参数是强制性的 当我调用 -UseRegions 时,-Region 是强制性的,其余的不是。当我不调用 -UseRegions 时,-Country 是强制性的,其余的则不是。

我一直在玩参数集,但我永远无法解决它。我也不想有一个名为-UseCountry 的额外参数,我想要-UseRegions 来控制-Region 和-Country。

有人有过类似的吗?

非常感谢提前!!!!

4

1 回答 1

0

我明白了,参数集有时甚至让我头晕目眩。听起来,你想要 3 个不同的集合。

  1. 国家
  2. 地区
  3. 发邮件

您可以使用 删除除-SendMail开关以外的所有内容mandatory = $true

function potato
{
    param (
        [Parameter  (
            Mandatory = $true,
            ParameterSetName = "SendMail",
            HelpMessage = "Call this parameter to send information mail ONLY."
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseRegion"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseCountry"
        )]
        [switch]
        $SendMail,



        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "SendMail"
        )]
        [Parameter  (
            Mandatory = $true,
            ParameterSetName = "UseRegion",
            HelpMessage = "Define the region to apply the function."
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseCountry"
        )]
        [ValidateSet("AMER","APAC","EMEA")]
        [string]
        $Region,



        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "SendMail"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseRegion"
         )]
        [Parameter  (
            Mandatory = $true,
            HelpMessage = "Define the country to apply the function.",
            ParameterSetName = "UseCountry"
        )]
        [ValidateNotNullOrEmpty()]
        [string]
        $Country,
        


        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "SendMail"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseRegion"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseCountry"
        )]
        [Parameter  (
            Mandatory = $false,
            HelpMessage = "Define months for a machine to be outdated. Default value is 3."
        )]
        [ValidateRange(1, [int]::MaxValue)]
        [int]
        $Months = 3,



        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "SendMail"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseRegion"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseCountry"
        )]
        [Parameter  (
            Mandatory = $false,
            HelpMessage = "Define days for a machine to be on the ""Disabled"" OU before permanent deletion. Minimum and default value is 7."
        )]
        [ValidateRange([int] 7, [int] 30)]
        [int]
        $DaysToDeletion = 7,



        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "SendMail"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseRegion"
        )]
        [Parameter  (
            Mandatory = $false,
            ParameterSetName = "UseCountry"
        )]
        [Parameter  (
            Mandatory = $false,
            HelpMessage = "Define operation to exclude from the rule. I.E.: Operations that does not connect to the VPN."
        )]
        [string]
        $Exclude
    )
}

现在,如果您执行help potatoSYNTAX 部分将显示此

    potato -Country <string> [-SendMail] [-Region {AMER | APAC | EMEA}] [-Months <int>] [- 
    DaysToDeletion <int>] [-Exclude <string>]  [<CommonParameters>]
    
    potato -Region {AMER | APAC | EMEA} [-SendMail] [-Country <string>] [-Months <int>] [- 
    DaysToDeletion <int>] [-Exclude <string>]  [<CommonParameters>]
    
    potato -SendMail [-Region {AMER | APAC | EMEA}] [-Country <string>] [-Months <int>] [- 
    DaysToDeletion <int>] [-Exclude <string>]  [<CommonParameters>]
于 2020-10-13T17:22:59.913 回答