您好,我正在尝试创建一个动态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
调用特定文件的包装器。
答案 0 :(得分:1)
我认为你还需要一条线。您永远不会将main()
添加到$ParameterAttribute
。您可以使用此行$AttributeCollection
执行此操作。
$AttributeCollection.Add($ParameterAttribute)