微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Powershell中的hastable属性上执行where对象?

如何解决如何在Powershell中的hastable属性上执行where对象?

在我的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管道之外)。

我希望我足够清楚。

在此先感谢您的帮助!

解决方法

您似乎想通过$arrayCounts哈希表数组中的Ip条目值交叉引用$resultHash数组中的哈希表。

根据您的代码,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 ,那么您的代码将如下所示:

# 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
  }
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。