Quantcast
Channel: /r/powershell – How to Code .NET
Viewing all articles
Browse latest Browse all 8793

refactoring parameter to DynamicParam

$
0
0

This is my first attempt at working with DynamicParam(primarily for an autocomplete functionality) and I am getting a bit lost. I am trying to make the param be bound to the code:

Get-ChildItem -filter *-* | ?{$_.PSIsContainer } | %{($_.Name.split('-'))[1]} | Sort-Object 

My parameter block is (based on https://foxdeploy.com/2017/01/13/adding-tab-completion-to-your-powershell-functions/ )

Param([parameter()] [String] $TicketDir = (Get-Location).Path, [parameter(Mandatory=$true)] [String] $Customer 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] } }, [switch]$Visual ) 

This however is giving me a missing ‘)’ error just behind the DynamicParam declaration, but I am not seeing it. There may also be other errors as I have not done this before. Could someone please assist and give information to get this block working?

submitted by /u/coderwolf
[link] [comments]

The post refactoring parameter to DynamicParam appeared first on How to Code .NET.


Viewing all articles
Browse latest Browse all 8793

Trending Articles