2017-12-05 12 views
0

こんにちは、私は、自分のコマンドレットパラメータの1つの単純なテキストファイルの内容に基づいて動的なValidateSetを作成しようとしています。私はこのブログの記事https://blogs.technet.microsoft.com/pstips/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/に続いて、私は次のを思い付いた:パラメータセット '__AllParameterSets'で動的パラメータを指定できません

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を入力して作業をすることから始めた場合項目と、私は次のエラーを取得コマンドを実行します。具体的にGet-Contentコール単なるラッパーであるライン$serviceGroups = Get-NetScalerWhiteListについては

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 

をファイル。

答えて

1

もう1行は必要だと思います。 $AttributeCollectionには$ParameterAttributeを追加しないでください。この行を使用して行うことができます$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 

} 

} 
+0

ショーンさん、ありがとうございました!私はそれが私の目の前になければならないと思った –

関連する問題