【问题标题】:how to do a where-object on an hastable property in powershell?如何在powershell中对hastable属性执行where-object?
【发布时间】: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


    【解决方案1】:

    您似乎希望通过 $resultHash 哈希表数组中的 Ip 条目值交叉引用 $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 哈希表,你的代码应该是这样的:

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

    【讨论】:

    • 嗨 mkelement0,感谢您的帮助。我正在研究另一个主题,但我使用了你的代码并更改了我的哈希表的格式,它工作正常。
    • 很高兴听到这个消息,@shako;我的荣幸。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2012-01-15
    相关资源
    最近更新 更多