The [Hashtable []] parameter, which can come from a pipeline or an argument
Here's a function that takes an array of hashtables via an argument:
function abc () {
Param([Hashtable[]]$tables)
$tables.count
}
Usage example:
PS C:\> abc -tables @{ a = 10 }, @{ b = 20 }, @{ c = 30 }
3
Here's the function that accepts Hashtables through the pipeline:
function bcd () {
Param([parameter(ValueFromPipeline=$true)][Hashtable]$table)
$input.count
}
Usage example:
PS C:\> @{ a = 10 }, @{ b = 20 }, @{ c = 30 } | bcd
3
Is there a way to define a function that can take a hashtable array through an argument, or a pipeline through the same parameter? That is, a function that can be called in both of the ways shown above. Note that I will need the entire array of hash tables in one variable (hence the use $input
above in bcd
).
+3
dharmatech
source
to share
2 answers
function bcd () {
Param([parameter(ValueFromPipeline=$true)][Hashtable[]]$table)
Begin {$tables= @()}
Process {$tables += $table}
End {$tables.count}
}
@{ a = 10 }, @{ b = 20 }, @{ c = 30 } | bcd
bcd -table @{ a = 10 }, @{ b = 20 }, @{ c = 30 }
3
3
+4
mjolinor
source
to share
Here is my structure for two parameter (pipeline and cmd lines) parameters:
Function bcd ()
{
Param(
[parameter(ValueFromPipeline = $true)]
[Hashtable[]]$table
)
Process
{
ForEach ($tab in $table)
{
# do something with the table
}
}
}
+3
Mike shepard
source
to share