Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use filter with az ad app to do a bulk delete

I'm trying to delete a lot of apps that carry a similar property with AZ AD CLI. I can't find any good examples on --filter

Attempting to do something like this:

ad az app list --filter (displayName like 'stack') | ad az app delete

Any pointers greatly appreciated.

like image 316
Derek Avatar asked May 22 '19 17:05

Derek


2 Answers

You can use --filter like this

az ad app list --filter "startswith(displayName,'MyCommonPattern')"

Above mentioned command may give you quite a bit of json in output.

You can boil it down to just the appIds or whatever you need using --query like this

az ad app list --filter "startswith(displayName,'RohitCommonPattern')" --query '[].appId'

Sample output

[
  "b5exxxc4-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "f13xxxa5-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
]

I have shown this example using startswith but you could use other operators as well like eq, any for collections, logical operators like and, or. Do look at the link related to Azure AD Graph API for more samples.

One more thing that I did try but isn't probably supported is contains

More information

  • --filter accepts OData filter as per Microsoft Docs - az ad app list
  • Azure AD Graph API would probably get used behind the scenes to work with application listing, so I guess you could read about filtering and examples from here. Supported Query Options - Azure AD Graph API
  • Here is a general specification, although not everything may be implemented behind the scenes.

    NOTE: I have intentionally mentioned older Azure AD Graph API https://graph.windows.net and not the newer Microsoft Graph API https://graph.microsoft.com since Application related APIs are still in beta for Microsoft Graph API.

like image 94
Rohit Saigal Avatar answered Nov 09 '22 20:11

Rohit Saigal


To follow this up from Rohit's literally perfect answer, I added a quick BASH script to accomplish my loop:

for fn in `az ad app list --filter "startswith(displayName, 'Azure Stack')" --query '[].appId'`; do az ad app delete --id $fn; done
like image 44
Derek Avatar answered Nov 09 '22 20:11

Derek