Search code examples
azure-cli

Azure CLI - get Resource Group Id


I'm trying to obtain the Resource Group Id using Azure CLI using the RG name.

I have tried the following but to no avail:

$scopeId = (az group list --query $exemption.ResourceGroupName).Id

and:

$scopeId = (az group list --query "[?name==$exemption.ResourceGroupName]").Id

The underlying Json in the exemption object is as follows (values removed or replaced):

[    
      {
        "scope": "Resource Group",
        "scopeName": "",
        "scopeNameFull": "/providers/Microsoft.Management/managementgroups/",
        "exemptionCategory": "Waiver",
        "name": "",
        "description": "My Exemption description",
        "expiresOn": null,
        "ResourceName": null,
        "ResourceGroupName": "rg",
        "subscriptionId": "123",
        "policyDefinitionReferenceIds": [ "policy1", "policy2"]
      }      
]

Solution

  • After reproducing from my end, I could able to get this done using the below script.

    $a=az group list --query "[?name=='<YOUR_RESOURCE_GROUP_NAME>']" |ConvertFrom-Json
    $a.id
    

    Results:

    enter image description here