【问题标题】:How can I query JSON records in PowerShell如何在 PowerShell 中查询 JSON 记录
【发布时间】:2017-01-21 22:24:11
【问题描述】:

我在 PowerShell 中调用 REST api,它返回一些 JSON

$records = Invoke-RestMethod $uri -Headers $headers

如果我使用 .data 属性,我会得到我假设的 PowerShell 数组对象,因为我可以检查 $records.data.count 并得到预期的记录数。检查其中一条记录,例如$records.data[5]

id            : 123
zone_id       : example.com
parent_id     : 
name          : z1
content       : 1.2.3.4
ttl           : 3600
priority      : 
type          : A
regions       : {global}
system_record : False
created_at    : 2015-01-10T02:33:46Z
updated_at    : 2015-01-10T02:33:46Z

我想获取 typeA 的所有记录。我试过$records.data.Where($_.type -eq 'A') 但这给了我一个类型异常。我应该使用什么查询来根据属性值进行过滤?

【问题讨论】:

    标签: json powershell


    【解决方案1】:

    我试过 $records.data.Where($_.type -eq 'A') 但这给了我一个类型异常。

    Where() 扩展方法将ScriptBlock 作为其第一个参数(注意花括号):

    $records.data.Where({$_.type -eq 'A'})
    

    【讨论】:

      猜你喜欢
      • 2016-11-10
      • 2011-04-28
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 2011-02-09
      相关资源
      最近更新 更多