2017-12-05 1 views
0

안녕하세요, 내 cmdlet 매개 변수 중 하나에 대한 간단한 텍스트 파일의 내용을 기반으로하는 동적 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

줄이 하나 더 필요하다고 생각합니다. $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

숀 감사합니다. 나는 그것이 내 앞에서 바로 있어야한다고 생각했다. –

관련 문제