PowerShell 查询哈希表的嵌套子元素

PowerShell to query nested child element of hashtable

在 PowerShell 中,我想统计所有使用球的运动。挑战在于它们的级别低于我可以通过直接 Where-Object 查询访问的级别。这是哈希表:

$league = @{
   school = @{
      name = 'Lincoln High School' 
      levels = ('9','10','11','12')
      } 
   sports = @{
      football = @{ 
         coed = 'b'
         season = 'fall'
         balls = $true
      }
      hockey = @{ 
         coed = 'b'
         season = 'winter'
         balls = $false
      }
      lacrosse = @{ 
         coed = 'g'
         season = 'spring'
         balls = $true
      }
      swim = @{ 
         coed = 'c'
         season = 'winter'
         balls = $false
      }
   }
}

如何设计查询以跳至 balls?每项运动在关键级别的名称不同,通配符似乎不起作用。

( $league.sports | Where-Object { $_.[?].balls -eq $true } ).count

我需要一些东西来连接这项运动的儿童元素。

使用.where(..)

($league['sports'].GetEnumerator().Where{$_.Value.Balls}).Count

使用Where-Object:

($league['sports'].GetEnumerator() | Where-Object {$_.Value.Balls}).Count

如您所见,这两种方法都需要使用 .GetEnumerator() method

另一个更详细的备选方案:

$sports = $league['sports']
$sports.PSBase.Keys.Where{$sports[$_]['balls']}.Count