I have a function with a dynamic parameter and a regular parameter.
The dynamic parameter is named Role and the regular one is named RolePattern.
They are part of different sets. The default set is using the dynamic parameter.
If I try to use the set with the dynamic parameter, Powershell thinks I used the regular one.
If I change the name of the regular parameter to something that doesn't start with Role, it works fine. What am I doing wrong ?
Here is the test script:
function Test-ParameterConflict {
[CmdletBinding(DefaultParameterSetName = 'Dynamic')]
[OutputType()]
Param (
[Parameter(Mandatory = $false, ParameterSetName = 'Pattern')]
[ValidateNotNullOrEmpty()]
[string] $RolePattern
)
dynamicParam {
# Create the dictionary
$runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Set the dynamic parameter's name
$parameterName = 'Role'
# Create and set the parameter's attributes
$parameterAttribute = New-Object System.Management.Automation.ParameterAttribute
$parameterAttribute.Mandatory = $true
$parameterAttribute.ValueFromPipeline = $true
$parameterAttribute.ValueFromPipelineByPropertyName = $true
$parameterAttribute.ParameterSetName = 'Dynamic'
# Create the collection of attributes
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add the attributes to the attributes collection
$attributeCollection.Add($parameterAttribute)
# Generate and set the ValidateSet
$validValues = @('Role1', 'Role2', 'Role3')
$validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($validValues)
# Add the ValidateSet to the attributes collection
$attributeCollection.Add($validateSetAttribute)
# Create and add the dynamic parameter to the dictionary
$runtimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($parameterName, [string[]], $attributeCollection)
$runtimeParameterDictionary.Add($parameterName, $runtimeParameter)
return $runtimeParameterDictionary
}
Process {
"ParameterSetName = $($PSCmdlet.ParameterSetName)"
$PSCmdlet.MyInvocation.BoundParameters
}
}
So basically the syntax is:
Test-ParameterConflict [-RolePattern <string>]
Test-ParameterConflict -Role <string[]>
Here is the output:
Test-ParameterConflict -Role Role1 | ft -AutoSize
ParameterSetName = Pattern
Key Value
--- -----
RolePattern Role1