Passing a hashtable as an argument to a function in PowerShell

后端 未结 2 1863
悲&欢浪女
悲&欢浪女 2020-12-11 02:33

I have a problem in a PowerShell script:

When I want to pass a Hashtable to a function, this hashtable is not recognized as a hashtable.

function get         


        
2条回答
  •  忘掉有多难
    2020-12-11 03:03

    $Input is an automatic variable that enumerates the input given.

    Chose any other variable name and it'll work - although not necessarily as you might expect - to get the number of entries in a hashtable you need to inspect the Count property:

    function Get-Length {
        param(
            [hashtable]$Table
        )
    
        $Table.Count
    }
    

    Write-Output is implied when you just leave the $Table.Count as is.

    Also, the () suffix in the function name is unnecessary syntactic sugar with zero meaning when you declare your parameters inline with Param() - drop it

提交回复
热议问题