Matthias is right, but I would like to point out that there is a way to accept both types without using parameter sets.
Both types implement the IDictionary interface, so you can strictly enter your parameter using the interface, and then any type will be accepted (including custom types that you create or don't know yet) that implement the interface
function Do-Stuff { [CmdletBinding(DefaultParameterSetName='Ordered')] param( [Parameter(Mandatory=$true,Position=0,ParameterSetName='Ordered')] [System.Collections.IDictionary]$Dictionary ) $Dictionary.GetType().FullName }
This will take both:
C:\WINDOWS\system32\WindowsPowerShell\v1.0> do-stuff @{} System.Collections.Hashtable C:\WINDOWS\system32\WindowsPowerShell\v1.0> do-stuff ([ordered]@{}) System.Collections.Specialized.OrderedDictionary
Similarly, if you want to accept only an ordered dictionary (but not just a specific type of OrderedDictionary ), you can use the IOrderedDictionary interface, which is implemented by the aforementioned type, but not [hashtable] :
function Do-Stuff { [CmdletBinding(DefaultParameterSetName='Ordered')] param( [Parameter(Mandatory=$true,Position=0,ParameterSetName='Ordered')] [System.Collections.Specialized.IOrderedDictionary]$Dictionary ) $Dictionary.GetType().FullName }
Then:
C:\WINDOWS\system32\WindowsPowerShell\v1.0> do-stuff ([ordered]@{}) System.Collections.Specialized.OrderedDictionary C:\WINDOWS\system32\WindowsPowerShell\v1.0> do-stuff @{} Do-Stuff : Cannot process argument transformation on parameter 'Dictionary'. Cannot convert the "System.Collections.Hashtable" value of type "System.Collections.Hashtable" to type "System.Collections.Specialized.IOrderedDictionary". At line:1 char:10 + do-stuff @{} + ~~~ + CategoryInfo : InvalidData: (:) [Do-Stuff], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,Do-Stuff
source share