Skip to content

Commit 2b250f7

Browse files
committed
add force subscription placement for use in testing
1 parent 9c11d7a commit 2b250f7

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

src/ALZ/Public/Remove-PlatformLandingZone.ps1

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ function Remove-PlatformLandingZone {
151151
may have already been deleted but you still want to clean up subscriptions.
152152
Default: $false (exit with error if no management groups found)
153153
154+
.PARAMETER ForceSubscriptionPlacement
155+
A switch parameter that forces moving all subscriptions (provided via -Subscriptions or -AdditionalSubscriptions)
156+
to the management group specified in -SubscriptionsTargetManagementGroup. If -SubscriptionsTargetManagementGroup
157+
is not specified, the default management group is determined from the tenant's hierarchy settings
158+
(via az account management-group hierarchy-settings list), falling back to tenant root if no default is configured.
159+
Before moving, the function checks if each subscription is already under the target management group and skips
160+
the move if it is.
161+
Default: $false (do not force placement)
162+
154163
.EXAMPLE
155164
Remove-PlatformLandingZone -ManagementGroups @("alz-test") -AdditionalSubscriptions @("Bootstrap-Sub-001")
156165
@@ -329,7 +338,8 @@ function Remove-PlatformLandingZone {
329338
[string[]]$ManagementGroupsToDeleteNamePatterns = @(),
330339
[string[]]$RoleDefinitionsToDeleteNamePatterns = @(),
331340
[string[]]$DeploymentStacksToDeleteNamePatterns = @(),
332-
[switch]$AllowNoManagementGroupMatch
341+
[switch]$AllowNoManagementGroupMatch,
342+
[switch]$ForceSubscriptionPlacement
333343
)
334344

335345
function Write-ToConsoleLog {
@@ -1223,6 +1233,50 @@ function Remove-PlatformLandingZone {
12231233

12241234
$subscriptionsFinal = $subscriptionsFound.ToArray() | Sort-Object -Property name -Unique
12251235

1236+
# Force subscription placement if requested
1237+
if($ForceSubscriptionPlacement -and $subscriptionsFinal.Count -gt 0) {
1238+
$targetManagementGroupForPlacement = $SubscriptionsTargetManagementGroup
1239+
1240+
if(-not $targetManagementGroupForPlacement) {
1241+
# Get default management group from hierarchy settings
1242+
$tenantId = (az account show --query "tenantId" -o tsv)
1243+
$hierarchySettings = (az account management-group hierarchy-settings list --name $tenantId -o json 2>$null) | ConvertFrom-Json
1244+
if($hierarchySettings -and $hierarchySettings.value.defaultManagementGroup) {
1245+
$targetManagementGroupForPlacement = $hierarchySettings.value.defaultManagementGroup
1246+
Write-ToConsoleLog "No target management group specified, using default management group from hierarchy settings: $targetManagementGroupForPlacement" -IsWarning
1247+
} else {
1248+
# Fall back to tenant root if no default is configured
1249+
$targetManagementGroupForPlacement = $tenantId
1250+
Write-ToConsoleLog "No default management group configured in hierarchy settings, using tenant root: $targetManagementGroupForPlacement" -IsWarning
1251+
}
1252+
}
1253+
1254+
if($targetManagementGroupForPlacement) {
1255+
Write-ToConsoleLog "Force subscription placement enabled, moving subscriptions to management group: $targetManagementGroupForPlacement" -NoNewLine
1256+
1257+
$subscriptionsFinal | ForEach-Object -Parallel {
1258+
$subscription = $_
1259+
$targetMg = $using:targetManagementGroupForPlacement
1260+
$funcWriteToConsoleLog = $using:funcWriteToConsoleLog
1261+
${function:Write-ToConsoleLog} = $funcWriteToConsoleLog
1262+
$TempLogFileForPlan = $using:TempLogFileForPlan
1263+
1264+
Write-ToConsoleLog "Moving subscription to management group: $targetMg, subscription: $($subscription.Name) (ID: $($subscription.Id))" -NoNewLine
1265+
if($using:PlanMode) {
1266+
Write-ToConsoleLog `
1267+
"Moving subscription to management group: $targetMg, subscription: $($subscription.Name) (ID: $($subscription.Id))", `
1268+
"Would run: az account management-group subscription add --name $targetMg --subscription $($subscription.Id)" `
1269+
-IsPlan -LogFilePath $TempLogFileForPlan
1270+
} else {
1271+
az account management-group subscription add --name $targetMg --subscription $subscription.Id 2>&1 | Out-Null
1272+
Write-ToConsoleLog "Subscription placed in management group: $targetMg, subscription: $($subscription.Name) (ID: $($subscription.Id))" -NoNewLine
1273+
}
1274+
} -ThrottleLimit $ThrottleLimit
1275+
1276+
Write-ToConsoleLog "Forced subscription placement completed." -IsSuccess
1277+
}
1278+
}
1279+
12261280
if($subscriptionsFinal.Count -eq 0) {
12271281
Write-ToConsoleLog "No subscriptions provided or found, skipping resource group deletion..." -IsWarning
12281282
} else {

0 commit comments

Comments
 (0)