Arrays – function [hashtable []] parameter, which can be from pipeline or parameter

This is a function that accepts a hash table array through parameters:

function abc () {

    Param([Hashtable[]]$tables)

    $tables.count

}

Use example:

PS C:\> abc -tables @{ a = 10 },@{ b = 20 },@{ c = 30 }
3

This is a function that accepts hashtables through the pipeline:

function bcd () {

    Param([parameter(ValueFromPipeline=$true)][Hashtable]$table)

    $input.count

}

Use example:

PS C:\> @{ a = 10 },@{ c = 30 } | bcd
3

Is there a way to define functions that can accept hash table arrays through parameters or pipes through the same parameters? That is, functions that can be called in the two ways shown above Note that I need to use the entire hash table array in a single variable (so use $input above in BCD)

Solution

function bcd () {
function bcd () {

    Param([parameter(ValueFromPipeline=$true)][Hashtable[]]$table)

    Begin {$tables= @()}
    Process {$tables += $table}
    End {$tables.count}

}

@{ a = 10 },@{ c = 30 } | bcd
bcd -table @{ a = 10 },@{ c = 30 }

3
3
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>