Powershell - Parameter sets

230 views Asked by At

I have a function that have this parameters:

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
)

i'm trying to acomplish the following:

When i call -SendMail NO parameter is mandatory When i call -UseRegions, -Region is mandatory and the rest is not. When i don't call -UseRegions, -Country is mandatory and the rest is not.

I've been playing with parameter sets but i can never get it to resolve. I also don't want to have an extra parameter called -UseCountry, i want -UseRegions to controll both -Region and -Country.

Anyone had anything like it?

Thanks a lot in advance!!!!

1

There are 1 answers

1
PowerShellGuy On

I get it, parameter sets make even my head spin sometimes. What it sounds like, is that you want 3 different sets.

  1. Country
  2. Region
  3. SendMail

You can remove all but the -SendMail switch by using 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
    )
}

Now if you do a help potato the SYNTAX section will show this

    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>]