Skip to content

Instantly share code, notes, and snippets.

@krnese
Created March 19, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krnese/5153f7ce8dd0fe746d59701928a82d6f to your computer and use it in GitHub Desktop.
Save krnese/5153f7ce8dd0fe746d59701928a82d6f to your computer and use it in GitHub Desktop.
List Mg hiearchy
function Get-AzMgHierarchy {
[cmdletbinding()]
param (
[string]$mgGroupId
)
# Getting Azure connection context for the signed in user
$currentContext = Get-AzContext
# fetching new token
$azureRmProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = [Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient]::new($azureRmProfile)
$token = $profileClient.AcquireAccessToken($currentContext.Subscription.TenantId)
# Listing All Subscriptions to grab their tenantId's
$listDescendants = @{
Uri = "https://management.azure.com/providers/Microsoft.Management/managementGroups/$($mgGroupId)/descendants?api-version=2018-03-01-preview"
Headers = @{
Authorization = "Bearer $($token.AccessToken)"
'Content-Type' = 'application/json'
}
Method = 'GET'
}
$list = Invoke-RestMethod @listDescendants
$list.value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment