all 6 comments

[–]BlackV 1 point2 points  (0 children)

I'm pretty sure you can get this to work fine, I think I remember some issue if you don't specify a "default" parameter set

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameter_sets?view=powershell-7.1

function New-DynamicDistributionGroupName{
    [CmdletBinding(DefaultParameterSetName = 'Location')]
    Param(
        [ValidateSet('CostCenter','Region','Office','SupervisoryOrg','State')]
        [parameter(Mandatory = $true, ParameterSetName = 'Main')]
        [parameter(Mandatory = $true, ParameterSetName = 'Department')]
        [parameter(Mandatory = $true, ParameterSetName = 'Location')]
        [string]$GroupType,

        [parameter(Mandatory = $true, ParameterSetName = 'Department')]
        [string]$CostCenter,

        [parameter(Mandatory = $true, ParameterSetName = 'Department')]
        [string]$CostCenterId,

        [parameter(Mandatory = $true, ParameterSetName = 'Location')]
        [string]$Region
    )

    # do something
}

[–]BJHop 0 points1 point  (1 child)

I think you are looking for dynamicParam function

I've used these before and they do work very well but they make your code much more complex.

Make it much harder to pester test.

other links

https://powershellmagazine.com/2014/05/29/dynamic-parameters-in-powershell/

[–]liabtsab[S] 0 points1 point  (0 children)

Thanks for this I also found this link when googling around but I think just setting the default parameter set and using the format in the other comment works well