Get all Azure resource groups within the specific Azure management group like "ABC"

335 views Asked by At

I'm trying to accomplished this by trying some commands but when i start with "Get-AzResourceGroup" i get below error:

ERROR: Get-AzSubscription : Method 'get_SerializationSettings' in type 'Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient' from assembly 'Microsoft.Azure.PowerShell.Clients.ResourceManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation. At line:1 char:1

'Az.Accounts' verison is 2.13.0. I have also tried to downgrade this to 2.12.1 but not luck 'Az.Resources'verison is 6.10.0

Any powershell script you suggest will be highly appreciated.

2

There are 2 answers

0
Xam On BEST ANSWER

I was able to make it work with shorter script in lower version of PS as mentioned below:

Get-AzManagementGroup -GroupName <MgmtGroup name> -Recurse | ForEach-Object {  Get-AzResourceGroup} 
1
Venkat V On

Get all Azure resource groups within the specific Azure management group like "ABC"

To fetch all resource groups within specific Azure management group , Here is the updated PowerShell code.

Powershell

    $managementGroups = Get-AzManagementGroup
    $managementGroupNameToFind = "Samplegroup1"
    $foundManagementGroup = $managementGroups | Where-Object { $_.Name -eq $managementGroupNameToFind -or $_.DisplayName -eq $managementGroupNameToFind }
    if ($foundManagementGroup) {
        $subscriptions = Get-AzManagementGroupSubscription -GroupName $foundManagementGroup.Name
        $result = @()
        foreach ($subscription in $subscriptions) {
            Write-Host "Subscription Name: $($subscription.DisplayName)"
            $subscriptionId = ($subscription.Id -split '/')[-1]
            $set = Set-AzContext -SubscriptionId $subscriptionId
            $resourceGroups = Get-AzResourceGroup
            foreach ($resourceGroup in $resourceGroups) {
                $result += [PSCustomObject] @{
                    Subscription = $subscription.DisplayName
                    SubscriptionId = $subscriptionId
                    ResourceGroup = $resourceGroup.ResourceGroupName
                }
            }
        }
    
        $result | Format-Table -AutoSize
    }

If I ran the above script, it would retrieve all subscriptions and fetch all Resource Group details under the specified Azure Management Group. Output:

enter image description here