无法在参数集'__AllParameterSets'

时间:2017-12-05 16:32:33

标签: powershell cmdlets

您好,我正在尝试创建一个动态ValidateSet,它基于我的一个cmdlet参数的简单文本文件的内容。我关注了这篇博文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我的验证集就在那里工作,但是当我选择其中一个项目并运行命令时,我得到以下错误:

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调用特定文件的包装器。

1 个答案:

答案 0 :(得分:1)

我认为你还需要一条线。您永远不会将main()添加到$ParameterAttribute。您可以使用此行$AttributeCollection执行此操作。

$AttributeCollection.Add($ParameterAttribute)