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

630 Views Asked by At

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?

2

There are 2 best solutions below

0
Jeremy Thompson On BEST ANSWER

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.

1
VenkateshDodda On

Based on the shared information, I have understood that you want to know the

  • hosted resource group(in which resource group got created before move operation).
  • current resource group (currently where the resource resides in).

We don't have any direct cmdlets to pull this information.

You can make use of activity logs to see the resource move operation and pull the information of resources which were moved & respective target resource group names accordingly using the below cmdlet

Get-AzActivityLog -StartTime (get-date).AddDays(-90) -EndTime (get-date)| Where-Object {$_.Authorization.Action -like "Microsoft.Resources/subscriptions/resourceGroups/moveresources/action" -and $_.OperationName -like "Move Resource Group Resources" -and  $_.EventName -like "Begin request" } | Select -ExpandProperty Properties

Sample screenshot for your reference:

enter image description here

Note: Using Get-AzActivityLog cmdlet you can pull logs for only last 90days.