Dictionary graph and number of keys

I wanted to count the number of parameters that each Powershell cmdlet needed, and noticed something strange:

$a = Get-Command Test-Connection
$a.Parameters.Count

I would expect the result 13, but instead got an object, because the dictionary / hash table of the parameters really contains the key 'Count'( $a.Parameters.'Count').

How to differentiate a call to a property call Count(getter method) of a dictionary and access to an element $a.'Count'?

+4
source share
1 answer

I would look at the type of object. A bit awkward, but it will work something like this:

$B = (Get-Command Test-Connection).Parameters
if ($B.GetType().Name -eq "Dictionary``2") { $B.Keys.Count}
else {$B.Count}

However, to be honest, I don’t think you need to do this, but it really helps.

0
source

Source: https://habr.com/ru/post/1657673/


All Articles