Kaido Jarvemets - Logo

Visualizing Azure Management Group Hierarchies with PowerShell and Markdown

The Problem: Navigating Azure’s Complexity

As organizations expand their footprint in the cloud, managing resources across multiple Azure subscriptions can quickly become a daunting task. The inherent complexity of Azure’s structure, with its layers of subscriptions and management groups, presents a significant challenge in governance, compliance, and resource organization. Without a clear view of this hierarchy, administrators may struggle with applying consistent policies, leading to potential security gaps and inefficiencies.

The Solution: PowerShell Scripting Meets Markdown

To address this challenge, I introduce a two-part solution leveraging PowerShell and Markdown. My approach involves using PowerShell scripts to programmatically list all management groups, their child groups, and subscriptions, presenting this complex hierarchy in an easily understandable format. The first script, Get-ManagementGroupHierarchy, retrieves the hierarchy and displays it directly in the console. The second script, Get-ManagementGroupHierarchyMarkdown, formats this output as a Markdown document, providing a structured and shareable representation of your Azure environment.

Script 1: Retrieving the Hierarchy

The first script, Get-ManagementGroupHierarchy, is designed to list all management groups and their subscriptions. It outputs a clear, hierarchical view directly in the console, making it easy to understand the structure at a glance.

				
					# PowerShell script to list Azure management groups and subscriptions hierarchically
function Get-ManagementGroupHierarchy {
    param (
        [Parameter(Mandatory=$true)]
        [string]$ParentGroupId,
        [int]$Level = 0
    )

    $IndentPrefix = '--' # Define indentation prefix for visual hierarchy
    $Indent = $IndentPrefix * $Level

    if ($Level -eq 0) {
        $ParentGroup = Get-AzManagementGroup -GroupId $ParentGroupId
        Write-Output "Management Group: $($ParentGroup.DisplayName)"
    }

    $Subscriptions = Get-AzManagementGroupSubscription -GroupName $ParentGroupId
    foreach ($Sub in $Subscriptions) {
        Write-Output "** Subscription: $($Sub.DisplayName)"
    }

    $ChildGroups = Get-AzManagementGroup -GroupId $ParentGroupId -Expand
    if ($ChildGroups.Children) {
        foreach ($Child in $ChildGroups.Children) {
            if ($Child.Type -eq "Microsoft.Management/managementGroups") {
                Write-Output "$Indent Management Group: $($Child.DisplayName)"
                Get-ManagementGroupHierarchy -ParentGroupId $Child.Name -Level ($Level + 1)
            }
        }
    }
}

				
			

Script Output

Script 2: Formatting for Documentation

The second script, Get-ManagementGroupHierarchyMarkdown, formats the retrieved hierarchy into Markdown. This enables the creation of structured documentation that can be easily shared, understood, and integrated into project documentation or wikis.

				
					function Get-ManagementGroupHierarchyMarkdown {
    param (
        [Parameter(Mandatory=$true)]
        [string]$ParentGroupId,
        
        [int]$Level = 0
    )

    $Indent = '  ' * $Level # Two spaces per level for markdown indentation

    if ($Level -eq 0) {
        $ParentGroup = Get-AzManagementGroup -GroupId $ParentGroupId
        Write-Output "# Management Group: $($ParentGroup.DisplayName)"
    }

    # Fetch and list subscriptions for the current management group
    $Subscriptions = Get-AzManagementGroupSubscription -GroupName $ParentGroupId
    foreach ($Sub in $Subscriptions) {
        Write-Output "$Indent- **Subscription**: $($Sub.DisplayName)"
    }

    # Process any child management groups
    $ChildGroups = Get-AzManagementGroup -GroupId $ParentGroupId -Expand
    if ($ChildGroups.Children) {
        foreach ($Child in $ChildGroups.Children) {
            if ($Child.Type -eq "Microsoft.Management/managementGroups") {
                Write-Output "$Indent- Management Group: $($Child.DisplayName)"
                Get-ManagementGroupHierarchyMarkdown -ParentGroupId $Child.Name -Level ($Level + 1)
            }
        }
    }
}

# Example usage with a specific management group ID
Get-ManagementGroupHierarchyMarkdown -ParentGroupId "XXXXXXXXXXXXXXX"
				
			

Script Output

Why PowerShell and Markdown?

PowerShell is a powerful tool for automation and scripting within Azure, offering the flexibility to retrieve and manipulate Azure resource information programmatically. It allows us to navigate Azure’s complexity without manual intervention, ensuring accurate and up-to-date organizational insights.

Markdown, on the other hand, is a lightweight markup language that’s both easy to write and read. When combined with PowerShell, it offers a simple yet effective way to document the hierarchical structure of Azure management groups and subscriptions. This documentation can be easily shared, version-controlled, and integrated into existing documentation platforms, enhancing collaboration and governance practices.

Why This Matters

By automating the retrieval and documentation of Azure management group hierarchies, organizations can significantly improve their cloud governance framework. This method provides a clear, visual representation of the resource organization, making it easier to identify where policies should be applied, where resources are allocated, and how access is managed across the board. It supports better security practices by ensuring that no subscription falls outside the governance structure and aids in compliance efforts by documenting the organizational setup.

Moreover, this approach empowers Azure administrators to make informed decisions about resource management, policy application, and organizational changes within their Azure environment. It simplifies what was once a complex and time-consuming process, allowing teams to focus more on strategic initiatives rather than getting bogged down in manual governance tasks.

Conclusion

In conclusion, navigating Azure’s management hierarchy doesn’t have to be a complex endeavor. By harnessing the power of PowerShell and the simplicity of Markdown, we can create a more manageable and transparent Azure environment. This solution not only aids in the immediate need for clarity and organization but also sets the foundation for scalable and efficient cloud governance practices moving forward.

If you’ve found the solutions and insights shared here beneficial, please consider supporting this website and my work with a donation. Your contribution helps sustain and expand the resources available, enabling me to continue offering valuable content and tools. Thank you for your support!

Leave a Reply

Contact me

If you’re interested in learning about Visualizing Azure Management Group Hierarchies with PowerShell and Markdown. I can help you understand how this solution can benefit your organization and provide a customized solution tailored to your specific needs.

Table of Contents