Trying to get a better understanding of Dynamic Parameters, but something keeps bothering me. All examples I've found across the outer-webs (internet), the parameter attributes are always defined/included.
Here's what I'm working with:
Function Test-DynamicParameters {
[cmdletbinding()]
Param (
[System.IO.DirectoryInfo]$Path
)
DynamicParam
{
if ($Path.Extension) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $false
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new(
'TestSwitch', [switch], $attributeCollection
)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch', $dynParam1)
$paramDictionary
}
}
Begin { }
Process
{
$PSBoundParameters
}
End { }
}
. . .currently have tried numerous combinations of removing certain lines/code to see what would make my Dynamic Parameter show, but nothing works without the attributes being declared. Is this necessary?
Question: what is the simplest form of Dynamic Parameter declaration that I would need for it to work?
For Example - can it just be shortened to where I only define just the name? Or, would PowerShell insist a type is specified instead of defaulting to [object]
? Something like:
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch')
$paramDictionary