I have resource name , but I have to find resource group to which resource belongs. It needs to be done using az cli
You can use the command below to list resources by resource name :
az resource list -n '<resource name>'
So that you can find resourceGroup property in result:
In addition to other answers provided, if you wanted to filter the results of az resource list to show just the resource group name, you could query the json output for the resourceGroup key and then output as tab-separated values.
az resource list -n resource-name --query "[].resourceGroup" -o tsv
Which will just output like this:
ked#mac:~/repos|master ⇒ az resource list -n my-resource --query "[].resourceGroup" -o tsv
my-rg
Here's the azure documentation that shows how to format output.
Related
I need to export a list of Application Insights with two columns: Name and Instrumentation Key.
With the following command I get the names, but the column InstrumentationKey is always null.
az resource list --resource-type "Microsoft.Insights/components" --query "[].{Name:name, InstrumentationKey:properties.InstrumentationKey}"
Without filtering the columns, by running simply
az resource list --resource-type "Microsoft.Insights/components"
I can't find the Instrumentation Key.
What is the command to get the Instrumentation Key and relative name for all the resources in a subscription?
After reproducing from my end, I could able to get the expected results using az monitor app-insights component show. Below is complete Query that worked for me.
$a = az monitor app-insights component show | ConvertFrom-Json
$a | Select-Object name,instrumentationKey
RESULTS:
This command gives all NIC names in the Resource group
az network nic list --resource-group "RG_TEST" --query "[0].name"
I want to get only NIC names contains the below string
String: NIC_PROD_TEST
After my workaround around this, I was able to retrieve results for a particular string by using the below query.
Eg: String: 'new'
Use "query "[?contains(name, 'string')].name" as shown below:
az network nic list --resource-group <resourcegroupName> --query "[?contains(name, 'new')].name"
Output:
If you want to get nic name starts with a particular string, you can use
"query "[?starts_with(name, 'string')].name"
az network nic list --resource-group <resourcegroupName --query "[?starts_with(name, 'new')].name"
Output:
Reference: MSDoc
I am query for resources in a subscription as follows:
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'] "| ConvertFrom-Json)
But i want to put a second query parameter like the following
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'&& resourcegroup== $resourcegroup.name ]"|
ConvertFrom-Json)
How can i do this ?
For querying multiple parameters you can use az graph query in azure cli as below and I followed Microsoft-Document:
Firstly, Set account subscription as below:
az account set --name "XX"
XX- Subscription name.
Now query to get resources:
az graph query -q "Resources | where type=~ 'microsoft.web/sites' | project resourceGroup, name"| ConvertFrom-Json
Please try by using the Pipe expression (|) to separate each condition. Your command would be something like:
(az resource list --subscription <subscription-id> --query "[?
type=='Microsoft.Web/sites'] | [?resourceGroup=='$resourcegroup.name']" |
ConvertFrom-Json)
Please ensure that you are using proper casing for property names (e.g. resourceGroup instead of resourcegroup) and string values are inside single quotes.
Here's the command I used to get storage accounts in a resource group in my Azure Subscription:
az resource list --query "[? type=='Microsoft.Storage/storageAccounts'] | [?resourceGroup=='my-resource-group-name']"
I need this to write a bash script. I have a bunch of resource IDs and I need to find the resource groups to which they belong. Is there someway to do this?
The RG name is included in the ID. The fully qualified ID of the resource, including the resource name and resource type. Use the format, /subscriptions/{guid}/resourceGroups/{resource-group-name}/{resource-provider-namespace}/{resource-type}/{resource-name} Part of the resource ID is the resource group name.
Use az resoure show, to get the values of each resource
az resource show --ids /subscriptions/0b1f6471-1bf0-4dda-aec3-111111111111/resourceGroups/MyResourceGroup/providers/Microsoft.Web/sites/MyWebapp
If you have many ids, you could write a for loop
If you want to write a query you could do JMESPath like this:
az resource list --query [?id=='the-id'].resourceGroup
Or with az resource show --ids
az resource show --ids "the-id" --query properties.actionGroups.groupIds
You still have to loop through the ids though
Is there a way using Azure CLI to list subscriptions where I have a specific role (owner for instance).
Currently 'az account list --output table' shows all subscriptions I'm associated with.
Thank you all!
You can use list role assignments for a user with the command,
az role assignment list --assignee {assignee}
By default, only role assignments for the current subscription will be displayed. To view role assignments for the current subscription and below, add the --all parameter.
You can get the expected subscription lists like this,
#!/bin/bash
subIds=$(az account list --query "[].id" -o tsv)
user="example#contoso.com"
for subId in $subIds
do
filteredScope=$(az role assignment list --assignee $user --all --query "[?roleDefinitionName == 'Owner'].scope" --subscription "$subId" -o tsv)
echo ${filteredScope##*/}
done
I tried it on the windows subsystem for Linux.