mirror of
https://dev.azure.com/effectory/Survey%20Software/_git/Cloud%20Engineering
synced 2026-02-27 18:52:18 +01:00
113
Powershell/Lists/Azure/ManagementGroups.ps1
Normal file
113
Powershell/Lists/Azure/ManagementGroups.ps1
Normal file
@@ -0,0 +1,113 @@
|
||||
#Connect-AzAccount
|
||||
|
||||
class ResourceCheck {
|
||||
[string] $SubscriptionId = ""
|
||||
[string] $SubscriptionName = ""
|
||||
[string] $SubscriptionState = ""
|
||||
[string] $Level0_ManagementGroupId = ""
|
||||
[string] $Level1_ManagementGroupId = ""
|
||||
[string] $Level2_ManagementGroupId = ""
|
||||
[string] $Level0_ManagementGroupName = ""
|
||||
[string] $Level1_ManagementGroupName = ""
|
||||
[string] $Level2_ManagementGroupName = ""
|
||||
}
|
||||
|
||||
Write-Host "======================================================================================================================"
|
||||
Write-Host "Creating list of Effectory Management Groups and subscriptions."
|
||||
Write-Host "- Note: not very dynamic; Starts at hard coded root group and works up max 2 levels."
|
||||
Write-Host "======================================================================================================================"
|
||||
|
||||
[string] $date = Get-Date -Format "yyyy-MM-dd HHmm"
|
||||
$fileName = ".\$date azure_managementgroups.csv"
|
||||
[ResourceCheck[]]$Result = @()
|
||||
|
||||
$rootManagementGroup = (Get-AzManagementGroup -GroupId 'e9792fd7-4044-47e7-a40d-3fba46f1cd09' -Expand)[0]
|
||||
|
||||
#level 0
|
||||
Write-Host "---------------------------------------------------------------------------------------------"
|
||||
Write-Host "Level 0 Management group [$($rootManagementGroup.Name)]"
|
||||
Write-Host "---------------------------------------------------------------------------------------------"
|
||||
|
||||
$subscriptions = $rootManagementGroup.Children | Where-Object Type -EQ '/subscriptions'
|
||||
|
||||
foreach ($subscription in $subscriptions)
|
||||
{
|
||||
$scope = $subscription.Id.Substring($subscription.Parent.Length, $subscription.Id.Length - $subscription.Parent.Length)
|
||||
$subscriptionId = $scope.Replace("/subscriptions/", "")
|
||||
Write-Host "Subscription [$($subscription.DisplayName) - $subscriptionId]"
|
||||
|
||||
[ResourceCheck] $resourceCheck = [ResourceCheck]::new()
|
||||
$resourceCheck.Level0_ManagementGroupId = $rootManagementGroup.Id
|
||||
$resourceCheck.Level0_ManagementGroupName = $rootManagementGroup.DisplayName
|
||||
$resourceCheck.SubscriptionId = $subscriptionId
|
||||
$resourceCheck.SubscriptionName = $subscription.DisplayName
|
||||
$resourceCheck.SubscriptionState = $subscription.State
|
||||
$Result += $resourceCheck
|
||||
}
|
||||
|
||||
#level 1
|
||||
foreach ($level1ManagementGroupLister in ($rootManagementGroup.Children | Where-Object Type -EQ 'Microsoft.Management/managementGroups'))
|
||||
{
|
||||
$level1ManagementGroup = (Get-AzManagementGroup -Group $level1ManagementGroupLister.Name -Expand)[0]
|
||||
|
||||
Write-Host " ---------------------------------------------------------------------------------------------"
|
||||
Write-Host " Level 1 Management group [$($level1ManagementGroup.Name)]"
|
||||
Write-Host " ---------------------------------------------------------------------------------------------"
|
||||
|
||||
$subscriptions = $level1ManagementGroup.Children | Where-Object Type -EQ '/subscriptions'
|
||||
|
||||
foreach ($subscription in $subscriptions)
|
||||
{
|
||||
$scope = $subscription.Id.Substring($subscription.Parent.Length, $subscription.Id.Length - $subscription.Parent.Length)
|
||||
$subscriptionId = $scope.Replace("/subscriptions/", "")
|
||||
Write-Host " Subscription [$($subscription.DisplayName) - $subscriptionId]"
|
||||
|
||||
[ResourceCheck] $resourceCheck = [ResourceCheck]::new()
|
||||
$resourceCheck.Level0_ManagementGroupId = $rootManagementGroup.Id
|
||||
$resourceCheck.Level0_ManagementGroupName = $rootManagementGroup.DisplayName
|
||||
$resourceCheck.Level1_ManagementGroupId = $level1ManagementGroup.Id
|
||||
$resourceCheck.Level1_ManagementGroupName = $level1ManagementGroup.DisplayName
|
||||
$resourceCheck.SubscriptionId = $subscriptionId
|
||||
$resourceCheck.SubscriptionName = $subscription.DisplayName
|
||||
$resourceCheck.SubscriptionState = $subscription.State
|
||||
$Result += $resourceCheck
|
||||
}
|
||||
|
||||
#level 2
|
||||
foreach ($level2ManagementGroupLister in ($level1ManagementGroup.Children | Where-Object Type -EQ 'Microsoft.Management/managementGroups'))
|
||||
{
|
||||
$level2ManagementGroup = (Get-AzManagementGroup -Group $level2ManagementGroupLister.Name -Expand)[0]
|
||||
|
||||
Write-Host " ---------------------------------------------------------------------------------------------"
|
||||
Write-Host " Level 2 Management group [$($level2ManagementGroup.Name)]"
|
||||
Write-Host " ---------------------------------------------------------------------------------------------"
|
||||
|
||||
$subscriptions = $level2ManagementGroup.Children | Where-Object Type -EQ '/subscriptions'
|
||||
|
||||
foreach ($subscription in $subscriptions)
|
||||
{
|
||||
$scope = $subscription.Id.Substring($subscription.Parent.Length, $subscription.Id.Length - $subscription.Parent.Length)
|
||||
$subscriptionId = $scope.Replace("/subscriptions/", "")
|
||||
Write-Host " Subscription [$($subscription.DisplayName) - $subscriptionId]"
|
||||
|
||||
[ResourceCheck] $resourceCheck = [ResourceCheck]::new()
|
||||
$resourceCheck.Level0_ManagementGroupId = $rootManagementGroup.Id
|
||||
$resourceCheck.Level0_ManagementGroupName = $rootManagementGroup.DisplayName
|
||||
$resourceCheck.Level1_ManagementGroupId = $level1ManagementGroup.Id
|
||||
$resourceCheck.Level1_ManagementGroupName = $level1ManagementGroup.DisplayName
|
||||
$resourceCheck.Level2_ManagementGroupId = $level2ManagementGroup.Id
|
||||
$resourceCheck.Level2_ManagementGroupName = $level2ManagementGroup.DisplayName
|
||||
$resourceCheck.SubscriptionId = $subscriptionId
|
||||
$resourceCheck.SubscriptionName = $subscription.DisplayName
|
||||
$resourceCheck.SubscriptionState = $subscription.State
|
||||
$Result += $resourceCheck
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$Result | Export-Csv -Path $fileName -NoTypeInformation
|
||||
|
||||
|
||||
Write-Host "============================================================================================="
|
||||
Write-Host "Done."
|
||||
|
||||
Reference in New Issue
Block a user