如何在 powershell 中对 hastable 属性 执行 where-object?

how to do a where-object on an hastable property in powershell?

在我的 foreach 中,我想对另一个对象变量执行 where-object 过滤器。

具体来说,下面是我的对象数组 ($arrayCounts) 的两个实例:

[0]: [Hastable: 2]
  [0]: [Occurences, 6]
    Key: "Occurences"
    Value: 6
  [1]: [Ip, "10.10.10.10"]
    Key: "Ip"
    Value: "10.10.10.10
[1]: [Hastable: 2]
  [0]: [Occurennces, 3]
    Key: "Occurences"
    Value: 3
  [1]: [Ip, "10.10.10.11"]
    Key: "Ip"
    Value: "10.10.10.11"

这里是我要执行 where-object 的循环:

foreach ($result in $resultHash.GetEnumerator()) {
$currentCountResultObject = @{

    Ip         = $result.Key
    Legitimacy = $result.Value
    Occurences = $arrayCounts.Occurences.Value | Where-Object ($result.Key -eq $arrayCounts.Ip.Value)  
    }
    $countResultObject += $currentCountResultObject
}

'Ip' 和 'Legitimacy' 成员已完成,但 'Occurences' 仍然是空的,因为我的 where-object 表达式错误。

预期输出为:

[0]: [Hastable: 3]
  [0]: [Legitimacy, "legitimate"]
    Key: "Legitimacy"
    Value: "legitimate"
  [1]: [Ip, "10.10.10.10"]
    Key: "Ip"
    Value: "10.10.10.10"
  [2]: [Occurences, 3]
    Key: "Occurences"
    Value: 3
[1]: [Hastable: 3]
  [0]: [Legitimacy, "unknown"]
    Key: "Legitimacy"
    Value: "unknown"
  [1]: [Ip, "10.10.10.11"]
    Key: "Ip"
    Value: "10.10.10.11"
  [2]: [Occurences, 28]
    Key: "Occurences"
    Value: 28

where-object 的目的是为每个 IP 提供它出现的次数,并且此信息仅在我的变量 $arrayCounts 中可用(在我当前的 foreach 管道之外)。

希望我说得够清楚了。

提前感谢您的帮助!

您似乎想通过 $resultHash 哈希表数组中的 Ip 条目值 cross-reference $arrayCounts 数组中的哈希表。

根据您的代码,Where-Object 命令需要如下所示:

Occurrences = ($arrayCounts | Where-Object { $_.Ip -eq $result.Key }).Occurrences

也就是说,您必须枚举数组 $arrayCounts 的元素,它们是哈希表,并根据手头的 IP 地址检查每个哈希表的 Ip 条目。 此过滤操作的结果是匹配的哈希表(我假设 Ip 条目在输入哈希表中是唯一的),其 Occurrences 条目值包含所需的出现次数。

使用.Where() array method代替Where-Object cmdlet一般来说可以加快运行速度,但这里也是因为它允许您在找到匹配项后停止过滤(参数 'First'):

Occurrences = ($arrayCounts.Where({ $_.Ip -eq $result.Key }, 'First')).Occurrences

但是,根据所需的输出,我怀疑您对 $resultHash 变量的枚举存在缺陷。假设它也是哈希表的 数组 ,那么您的代码必须如下所示:

# Array of sample reference hashtables.
$arrayCounts = 
 @{ Occurrences = 6; Ip = "10.10.10.10" },
 @{ Occurrences = 3; Ip = "10.10.10.11" }
 
 # Array of sample result hashtables
 $resultHashes =
   @{ Legitimacy = 'legitimate'; Ip = "10.10.10.10" },
   @{ Legitimacy = 'unknown'; Ip = "10.10.10.11" }

# Loop over the hashtables in $resultHashes, construct a new hashtable
# with the cross-referenced information for each, and collect the
# result in a new hashtable array.
$finalResultHashes = 
  foreach ($resultHash in $resultHashes) {

    # Output a hashtable with that includes the matching Occurrence value
    # from the $arrayCounts array based on this result's IP.
    @{ 
      Ip = $resultHash.Ip
      Legitimacy = $resultHash.Legitimacy
      Occurrences = ($arrayCounts.Where({ $_.Ip -eq $resultHash.Ip }, 'First')).Occurrences
    }

  }

# Output the results for display.
# Note: I'm using JSON only for illustrative purposes.
$finalResultHashes | ConvertTo-Json

以上结果(JSON 仅用于阐明结果结构):

[
  {
    "Legitimacy": "legitimate",
    "Ip": "10.10.10.10",
    "Occurrences": 6
  },
  {
    "Legitimacy": "unknown",
    "Ip": "10.10.10.11",
    "Occurrences": 3
  }
]