@@ -28,7 +28,9 @@ function Remove-PlatformLandingZone {
2828 Use with extreme caution and ensure you have appropriate backups and authorization before executing.
2929
3030 . PARAMETER ManagementGroups
31- An array of management group IDs or names to process. By default, the function deletes child management groups
31+ An array of regex patterns to match against management group names or display names. The function queries
32+ all management groups in the tenant and matches each pattern against both the name and displayName properties.
33+ Multiple management groups can match a single pattern. By default, the function deletes child management groups
3234 one level below these target groups (not the target groups themselves). Use -DeleteTargetManagementGroups to
3335 delete the target groups as well. Subscriptions under these management groups will be discovered unless
3436 subscriptions are explicitly provided via the -Subscriptions parameter.
@@ -982,17 +984,25 @@ function Remove-PlatformLandingZone {
982984 }
983985
984986 Write-ToConsoleLog " Validating provided management groups..."
987+
988+ # Query all management groups in the tenant first
989+ $allManagementGroups = (az account management- group list -- query " [].{name:name,displayName:displayName}" - o json) | ConvertFrom-Json
990+
985991 foreach ($managementGroup in $ManagementGroups ) {
986- $managementGroupObject = (az account management- group show -- name $managementGroup ) | ConvertFrom-Json
992+ # Treat $managementGroup as a regex and match against name or displayName
993+ $matchingMgs = $allManagementGroups | Where-Object { $_.name -match $managementGroup -or $_.displayName -match $managementGroup }
987994
988- if ($null -eq $managementGroupObject ) {
989- Write-ToConsoleLog " Management group not found: $managementGroup " - IsWarning
995+ if ($null -eq $matchingMgs -or $matchingMgs .Count -eq 0 ) {
996+ Write-ToConsoleLog " Management group not found matching pattern : $managementGroup " - IsWarning
990997 continue
991998 }
992999
993- $managementGroupsFound += @ {
994- Name = $managementGroupObject.name
995- DisplayName = $managementGroupObject.displayName
1000+ foreach ($matchedMg in $matchingMgs ) {
1001+ Write-ToConsoleLog " Found management group matching pattern '$managementGroup ': $ ( $matchedMg.name ) ($ ( $matchedMg.displayName ) )" - NoNewLine
1002+ $managementGroupsFound += @ {
1003+ Name = $matchedMg.name
1004+ DisplayName = $matchedMg.displayName
1005+ }
9961006 }
9971007 }
9981008
0 commit comments