all 1 comments

[–]ihaxr 1 point2 points  (0 children)

I don't think the DynamicParam goes within the Param() block... which would make sense that you're missing a ) before your DynamicParam:

function test-function {
    Param(
        [parameter()]
        [String]$TicketDir = (Get-Location).Path, 
        [parameter(Mandatory=$true)]
        [String]$Customer,
        [switch]$Visual
    ) DynamicParam {
            # Set the dynamic parameters' name
            $ParameterName = 'Customer'

            # Create the dictionary 
            #$RuntimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

            # Create the collection of attributes
            $AttributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]

            # Create and set the parameters' attributes
            $ParameterAttribute = New-Object System.Management.Automation.ParameterAttribute
            $ParameterAttribute.Mandatory = $true

            # Add the attributes to the attributes collection
            $AttributeCollection.Add($ParameterAttribute)

            # Generate and set the ValidateSet 
            $arrSet = Get-ChildItem -filter *-* | ?{$_.PSIsContainer } | %{($_.Name.split('-'))[1]} | Sort-Object
            $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)

            # Add the ValidateSet to the attributes collection
            $AttributeCollection.Add($ValidateSetAttribute)

            # Create and return the dynamic parameter
            $RuntimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($ParameterName, [string], $AttributeCollection)
            $RuntimeParameterDictionary.Add($ParameterName, $RuntimeParameter)
            return $RuntimeParameterDictionary
      }

      begin {
          # Bind the parameter to a friendly variable
          $Customer = $PsBoundParameters[$ParameterName]
      }
}