Can I directly import these parameters to my function with their correct types with only value with respectively?
$appparams = @{
sname = "RandomAppName" # type: string
sdata = [byte]12 # type: Byte
sgroup = $null # type: NULL
}
CASE: not sending as respectively + not correct types
$arglist = ($appparams.GetEnumerator() | Sort-Object Name | % { "$($_.Value)" }) -join ','
Invoke-Function .... $arglist -> Invoke-Function .... 12,,RandomAppName
Expected: sending only value with respectively and without any interpolation
Invoke-Function .... $arglist -> Invoke-Function .... "RandomAppName",12,$null
It looks like you're trying to pass the values of your hashtable entries as positional arguments to
Invoke-Function.This contrasts with the usual hashtable-based splatting that uses named arguments, where the key of each entry implies the target parameter, and the value the argument.
In other words:
If
Invoke-Functiondeclared parameters named-sname,-sdata, and-sgroupwith matching data types, you could useInvoke-Function @appParamsto pass your hashtable as-is (except for using@instead of$, for splatting).Since the parameter binding would then based on names, the fact that a
[hashtable](@{ ... }) instance's entries are inherently unordered would then not a problem, and no sorting is needed.If you get to control the definition of
Invoke-Functionand it takes a known set of arguments (as opposed to an open-ended list of arguments not known in advance), it is best to make it declare parameters and use hashtable-based splatting, as described.If you cannot modify the target command and must pass arguments positionally:
First, as mclayton points out, you need an ordered hashtable (
[ordered]) if the definition order of the entries (values) must be maintained (see below for an alternative based on sorting by keys, as you've tried).Then you can use array-based splatting to positionally pass your hashtable's values:
Note:
If you want to pass the array of values as an array, i.e. as a single argument, simply replace
@with$, i.e. pass the array as-is:If you don't get to control the creation of the hashtable and must indeed use sorting by key, define
$appParameterValuesas follows:Output:
As for what you tried:
This:
stringifies all values due to use of string interpolation (
"...")due to
-join ','creates a single string value that is the,-separated list of all stringified values.This results both in loss of the original data types and loss of separate values, and would be seen by any target command as a single string argument, as described.