Donnie Lambert
Donnie Lambert

Reputation: 103

Dynamic Parameter cannot be specified in parameter set '__AllParameterSets'

Hello, I am trying to create a dynamic ValidateSet that is based on the contents of a simple text file for one of my cmdlet parameters. I followed this blog post https://blogs.technet.microsoft.com/pstips/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/, and I came up with the following:

function Remove-NetScalerWhiteListItem
{
[CmdletBinding()]
Param
(
)
DynamicParam 
{
    $ParameterName = "ServiceGroup"

    $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

    $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
    $ParameterAttribute.Mandatory = $true
    $ParameterAttribute.Position = 0
    $ParameterAttribute.DontShow = $false

    $serviceGroups = Get-NetScalerWhiteList

    $ValidateSetAtrribute = New-Object System.Management.Automation.ValidateSetAttribute($serviceGroups)

    $AttributeCollection.Add($ValidateSetAtrribute)

    $RunTimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

    $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    $RuntimeParameterDictionary.Add($ParameterName, $RunTimeParameter)


    $RuntimeParameterDictionary

}
Begin
{
    $ServiceGroup = $PSBoundParameters[$ParameterName] 
}
Process
{
    Copy-Item "$masterIgnoreFilePath\ingnore.txt" "$masterIgnoreFilePath\ingnore.bak"
    $serviceGroups = Get-NetScalerWhiteList
    $serviceGroups.Remove($serviceGroup)
    Write-Host $serviceGroups

}

}

This partially works, if I begin by typing Remove-NetScalerWhiteListItem -ServiceGroup my validation set is there and working, however when I select one of the items and run the command I get the following error:

Remove-NetScalerWhiteListItem : Parameter 'ServiceGroup' cannot be specified 
in parameter set '__AllParameterSets'.
At line:1 char:1
+ Remove-NetScalerWhiteListItem -servicegroup servicegroupname
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-
NetScalerWhiteListItem], ParameterBindingException
    + FullyQualifiedErrorId : ParameterNotInParameterSet,Remove-
NetScalerWhiteListItem

As for the line $serviceGroups = Get-NetScalerWhiteList that is just a wrapper around a Get-Content call to a specific file.

Upvotes: 0

Views: 501

Answers (1)

Shawn Esterman
Shawn Esterman

Reputation: 2342

I think you need one more line. You never add the $ParameterAttribute to the $AttributeCollection. You can do so by using this line $AttributeCollection.Add($ParameterAttribute).

function Remove-NetScalerWhiteListItem
{
[CmdletBinding()]
Param
(
)
DynamicParam 
{
    $ParameterName = "ServiceGroup"

    $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

    $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
    $ParameterAttribute.Mandatory = $true
    $ParameterAttribute.Position = 0
    $ParameterAttribute.DontShow = $false

    $serviceGroups = Get-NetScalerWhiteList

    $ValidateSetAtrribute = New-Object System.Management.Automation.ValidateSetAttribute($serviceGroups)

    $AttributeCollection.Add($ValidateSetAtrribute)
    $AttributeCollection.Add($ParameterAttribute)

    $RunTimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)

    $RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    $RuntimeParameterDictionary.Add($ParameterName, $RunTimeParameter)


    $RuntimeParameterDictionary

}
Begin
{
    $ServiceGroup = $PSBoundParameters[$ParameterName] 
}
Process
{
    Copy-Item "$masterIgnoreFilePath\ingnore.txt" "$masterIgnoreFilePath\ingnore.bak"
    $serviceGroups = Get-NetScalerWhiteList
    $serviceGroups.Remove($serviceGroup)
    Write-Host $serviceGroups

}

}

Upvotes: 1

Related Questions