From fffc0b7fa89b0336e6d814607e26dc98fd1d3970 Mon Sep 17 00:00:00 2001 From: Mike Tellinghuisen Date: Wed, 6 May 2026 21:06:18 -0700 Subject: [PATCH] fix: resolve management group display names to IDs Search-AzGraph -ManagementGroup requires the group ID, not the display name. When a user passes a display name like "Tenant Root Group", the script now queries Get-AzManagementGroup to resolve it to the actual ID before calling Search-AzGraph. Falls back to using the input as-is if resolution fails. Closes #87 Co-Authored-By: Claude Opus 4.6 (1M context) --- CLOUD/Get-AzureSizingInfo.ps1 | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/CLOUD/Get-AzureSizingInfo.ps1 b/CLOUD/Get-AzureSizingInfo.ps1 index e566c08..e89be0e 100644 --- a/CLOUD/Get-AzureSizingInfo.ps1 +++ b/CLOUD/Get-AzureSizingInfo.ps1 @@ -74,7 +74,8 @@ may take a long time when large blob stores are located. .PARAMETER ManagementGroups -A comma separated list of Azure Management Groups to gather data from. +A comma separated list of Azure Management Group IDs or display names to gather data from. +If a display name is provided, the script will attempt to resolve it to the management group ID. .PARAMETER GetKeyVaultAmounts Gets number of key vaults, and amount of certificates, keys, and secrets in each key vault. One must add the @@ -581,9 +582,22 @@ switch ($PSCmdlet.ParameterSetName) { $subs = @() foreach ($managementGroup in ($ManagementGroups -split ',' | ForEach-Object { $_.Trim() })) { try { + # Search-AzGraph requires the management group ID, not the display name. + # Attempt to resolve the input as a display name first. + $mgId = $managementGroup + try { + $allMGs = Get-AzManagementGroup -ErrorAction Stop + $match = $allMGs | Where-Object { $_.DisplayName -eq $managementGroup } + if ($match) { + $mgId = $match.Name + Write-Host "Resolved management group display name '$managementGroup' to ID '$mgId'" -ForegroundColor Green + } + } catch { + Write-Host "Could not query management groups to resolve display name, using '$managementGroup' as-is" -ForegroundColor Yellow + } $subscriptionNames = $(Search-AzGraph ` -Query "ResourceContainers | where type =~ 'microsoft.resources/subscriptions'" ` - -ManagementGroup $managementGroup -ErrorAction Stop).name + -ManagementGroup $mgId -ErrorAction Stop).name foreach ($subName in $subscriptionNames) { $sub = Get-AzSubscription -TenantId $context.Tenant.Id -SubscriptionName $subName -ErrorAction Stop $subs += $sub