在查询Jmespath上实现条件

发布于 2025-02-13 03:15:35 字数 269 浏览 2 评论 0原文

我有这个查询,这给我带来了KeyVault秘密的到期日期,我必须带来少于30天的

az keyvault secret show \
 --vault name "$keyvault" \
 --name "$secret" \
 --query "attributes.expires" - o tsv

日期

--query "attributes.expires < 30 days" - o tsv

I have this query which bring me the expiring dates of the keyvault's secret, I have to bring the dates that are less than 30 days

az keyvault secret show \
 --vault name "$keyvault" \
 --name "$secret" \
 --query "attributes.expires" - o tsv

How can I implement something like

--query "attributes.expires < 30 days" - o tsv

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2025-02-20 03:15:36

您将无法在JMespath中进行日期计算,但是您可以完美地让PowerShell这样做,并使用 get-date实用程序

(Get-Date).AddDays(30) | Get-Date -Format yyyy-MM-ddTHH:mm:ssK

对于Jmespath查询部分,您必须知道只能将过滤器应用于数组,而不是对象,这是您似乎拥有的。
但是,您可以使用函数 to_array 在您的对象上,然后使用 pipe Expression 仅获取数组的第一项 - | [0]

因此,如果Azure客户端未过滤的JSON返回看起来像是

{
  "attributes": {
    "expires": "2022-07-30T00:00:00+00:00"
  }
}

您的查询

--query "to_array(attributes)[?
  expires < '$((Get-Date).AddDays(30) | Get-Date -Format yyyy-MM-ddTHH:mm:ssK)'
] | [0].expires"

You won't be able to make the date computation in JMESPath, but you could perfectly let PowerShell do it, with the Get-Date utility.

(Get-Date).AddDays(30) | Get-Date -Format yyyy-MM-ddTHH:mm:ssK

For the JMESPath query part, you have to know that a filter can only be applied to an array, not to an object, which is what you seems to have.
You can overcome this, though, using the function to_array on your object, and, then, use a pipe expression to get only the first item of the array – | [0].

So, if the unfiltered JSON return of the Azure client looks like

{
  "attributes": {
    "expires": "2022-07-30T00:00:00+00:00"
  }
}

Then, this should be your query

--query "to_array(attributes)[?
  expires < '$((Get-Date).AddDays(30) | Get-Date -Format yyyy-MM-ddTHH:mm:ssK)'
] | [0].expires"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文