I recently had an issue that turned out to be caused by an expired service principal credential. I would like to have a simple way to get a list of all the expiration dates/expired sp credentials to avoid this problem in the future. I've been looking at the odata and jsonpath documentation but it keeps getting errors.
Currently I can run this command in AZ CLI:
az ad sp list --all --query "[*].[appId]" -o tsv | xargs -I 1 az ad sp credential list --id 1 --query "[].endDate" > end.txt
This generates a json array or all the endDate
s, however it does not save the appId
that corresponds to the endDate
and for service principals that do not have any credentials an error message like this is printed in the terminal:
ERROR: Can't find associated application id from '00000000-0000-0000-0000-000000000000'
There are 2 things I would like to solve.
1: To avoid the ERROR
messages described above by only getting the appId
s that have oauth2Permissions
. I've tried adding an any()
filter to achieve this:
az ad sp list --all --filter "oauth2permissions/any()" --query "[*].[appId]" -o tsv | xargs -I 1 az ad sp credential list --id 1 --query "[].endDate" > end.txt
But this and other variations I've tried all cause errors or state Collection open properties are not supported in this release.
What is the correct/best way of doing this?
2: To make use of the output end dates I would like the endDate to be accompanied by some information about the SP, at least the appId
. So rather than just having a list element like ["2021-05-27T15:39:26.667907+00:00"]
I would like the elements to be dictionaries similar to:
[
{
"appId": "00000000-0000-0000-0000-000000000000"
"endDate": "2021-05-27T15:39:26.667907+00:00"
}
]
This would be simple if the credential itself had the appId
field but it does not. How can I add information to the output?
Alternatively to addressing these 2 issues separately, is there a different tool/solution that will provide a similar final output in a single command?