无法在参数集“__AllParameterSets”中指定动态参数

Dynamic Parameter cannot be specified in parameter set '__AllParameterSets'

您好,我正在尝试为我的一个 cmdlet 参数创建一个基于简单文本文件内容的动态 ValidateSet。我关注了这个博客 post https://blogs.technet.microsoft.com/pstips/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/,我想出了以下内容:

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

}

}

这部分有效,如果我开始输入 Remove-NetScalerWhiteListItem -ServiceGroup 我的验证集在那里并且工作,但是当我 select 其中一个项目和 运行 命令时,我收到以下错误:

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

至于 $serviceGroups = Get-NetScalerWhiteList 行,它只是对特定文件的 Get-Content 调用的包装。

我想你还需要一行。您永远不会将 $ParameterAttribute 添加到 $AttributeCollection。您可以使用此行 $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

}

}