felipe mercado
felipe mercado

Reputation: 21

Implement a condition on a query JMESPATH

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

Upvotes: 2

Views: 459

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39159

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"

Upvotes: 1

Related Questions