Search code examples
azurepowershellazure-resource-group

How to get the Resource Group of a resource in Azure?


I have resources in a Resource Group in Azure and it wont let me move the resource because it is in a Resource Group that is not the hosting Resource Group even though it has been moved .

I have tried to run the command in Powershell az resource list but cant seem to see the hosting Resource Group of the resource.

Is there a command I can run in Powershell that will give the current Resource Group of the resource and the hosting Resource Group of the resource?


Solution

  • Is there a Powershell command that will give the current Resource Group of the resource?

    Yes its the Get-AzureRmResource command:

    $resourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}"
    
    $resource = Get-AzureRmResource -ResourceId $resourceId
    $resourceGroupName = $resource.ResourceGroupName
    

    Fortunately there's an alternative to avoid specifying the "ResourceGroupName" which you're trying to find and that's specifying the ResourceType..

    $resourceGroupName = Get-AzureRmResource -ResourceName {resourceName} -ResourceType {resourceType} | Select-Object -ExpandProperty ResourceGroupName
    

    Also note the -ExpandProperties parameter in the documentation to include the resource properties.