3

I have a problem with dynamic parameters:

function get-foo {
 [cmdletBinding()]
 param(
  [parameter(Mandatory=$true)]
  $Name
 )
 DynamicParam { 
if($Name -like 'c*') {
$Attributes = New-Object 'Management.Automation.ParameterAttribute' 
$Attributes.ParameterSetName = '__AllParameterSets'            
$Attributes.Mandatory = $false
$AttributesCollection = New-Object 'Collections.ObjectModel.Collection[Attribute]' 
$AttributesCollection.Add($Attributes) 
$Dynamic = New-Object -TypeName 'Management.Automation.RuntimeDefinedParameter' -ArgumentList @('pattern','system.object',$AttributeCollection)
$ParamDictionary = New-Object 'Management.Automation.RuntimeDefinedParameterDictionary'          
$ParamDictionary.Add("pattern", $Dynamic) 
$ParamDictionary 
 } 
}
 end {
   if($test) {
       return $Name -replace '\b\w',$test
   }
   $name
 }
}

It's detecting my pattern parameter but it returns an error;

ps c:\> get-foo -Name cruze -pattern 123
get-foo : Le paramètre « pattern » ne peut pas être spécifié dans le jeu de paramètres « __AllParameterSets 
».
Au niveau de ligne : 1 Caractère : 8
+ get-foo <<<<  -Name cruze -pattern u
    + CategoryInfo          : InvalidArgument: (:) [get-foo], ParameterBindingException
    + FullyQualifiedErrorId : ParameterNotInParameterSet,get-foo

How to fix the problem?

3
  • Could you please translate the error message? Commented Oct 18, 2012 at 10:17
  • @Roman Kuzmin: The parameter "pattern" can not be specified in the parameter set "__ AllParameterSets Commented Oct 18, 2012 at 10:24
  • Thank you. Sorry, I meant to edit your question by adding the translation. Commented Oct 18, 2012 at 10:28

1 Answer 1

1

Replace $AttributeCollection with $AttributesCollection ('s' is missing).

Use Set-StrictMode in order to catch such typos :)


As for the script, I think there are other issues, too. Here is the corrected and working version:

function get-foo {
    [cmdletBinding()]
    param(
        [parameter(Mandatory=$true)]
        $Name
    )
    DynamicParam {
        if ($Name -like 'c*') {
            $Attributes = New-Object 'Management.Automation.ParameterAttribute'
            $Attributes.ParameterSetName = '__AllParameterSets'
            $Attributes.Mandatory = $false
            $AttributesCollection = New-Object 'Collections.ObjectModel.Collection[Attribute]'
            $AttributesCollection.Add($Attributes)
            $Pattern = New-Object -TypeName 'Management.Automation.RuntimeDefinedParameter' -ArgumentList @('pattern', [string], $AttributesCollection)
            $ParamDictionary = New-Object 'Management.Automation.RuntimeDefinedParameterDictionary'
            $ParamDictionary.Add("pattern", $Pattern)
            $ParamDictionary
        }
    }
    end {
        if ($Name -like 'c*') {
            if ($Pattern.IsSet) {
                return $Name -replace '\b\w', $Pattern.Value
            }
        }
        $name
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks Roman, with adding 's' no error :) but script don't work it return always 'cop' ; "PS> get-foo -name cop -pattern x" any idea ? thanks
@Why It seem that $test it never be involved and got a value.
@Christian, youpi ....i found a problem, "if($PSBoundParameters.test) { # code } " thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.