Skip to content

Instantly share code, notes, and snippets.

@rcabr
Last active February 22, 2019 19:35
Show Gist options
  • Save rcabr/feee31e17e0b5d38e056eaf2c8a505d3 to your computer and use it in GitHub Desktop.
Save rcabr/feee31e17e0b5d38e056eaf2c8a505d3 to your computer and use it in GitHub Desktop.
Create Azure mg/subscription hierarchy Mermaid diagram
<#
.SYNOPSIS
Create a Mermaid diagram (https://mermaidjs.github.io/)
that reflects the Azure management group and subscription hierarchy.
.DESCRIPTION
This script assumes prerequisites:
1) The Az module is installed (https://docs.microsoft.com/en-us/powershell/azure/new-azureps-module-az)
2) Azure sign-in is completed (Connect-AzAccount)
3) The signed-in user has the required permissions to enumerate the management groups and subscriptions
.NOTES
Author: Reuben Cabrera
Script birth date: 2019-02-22
.EXAMPLE
Get-SubscriptionHierarchyDiagram.ps1 | Out-File diagram.mmd
#>
Param(
# The name of the root management group from which to begin
[Parameter(Mandatory=$true)]
[String]
$RootGroupName,
# Orient the tree diagram to be Top-Down (instead of Left-Right)
[Parameter(Mandatory=$false)]
[switch]
$OrientTopDown
)
# retrieve all the data
$m = Get-AzManagementGroup -GroupName $RootGroupName -Expand -Recurse
function WalkTree {
param (
$parent,
$entity
)
$entity | Select-Object `
@{Name="Type"; Expression={$entity.Type.split('/')[-1]}}, `
DisplayName, `
@{Name="FancyName"; Expression={
$prefix = "fa:fa-group"
if ($entity.Type -eq "/subscriptions") {
$prefix = "fa:fa-key"
}
$prefix + " " + $entity.DisplayName
}}, `
@{Name="Id"; Expression={$entity.Id -replace '[/]'}}, `
@{Name="ParentId"; Expression={
if ($null -eq $parent) {
$null
}
else {
$parent.Id -replace '[/]'
}
}}
foreach ($child in $entity.Children) {
WalkTree $entity $child
}
}
$orientation = If ($OrientTopDown) { "graph TD" } else { "graph LR" }
# unroll into dependency pairs
$list = WalkTree $null $m
$orientation
$list | ForEach-Object -Process {" $($_.Id)[$($_.FancyName)]"}
$list | Where-Object ParentId -NE $null | ForEach-Object -Process {" $($_.ParentId) --> $($_.Id)"}
" classDef managementGroups fill:#0ff"
" classDef subscriptions fill:#ff0"
$list | ForEach-Object -Process {" class $($_.Id) $($_.Type)"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment