【发布时间】:2021-01-16 12:10:27
【问题描述】:
在我的 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' 成员完成得很好,但由于我的 where-object 表达式错误,'Occurences' 保持空白。
预期的输出是:
[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 管道之外)。
我希望我已经足够清楚了。
提前感谢您的帮助!
【问题讨论】:
标签: powershell foreach where-clause