Skip to content

Commit 3b81de1

Browse files
feat: simplify confirmation mechanism (#516)
# Pull Request ## Description Simplify confirmation prompt ## License By submitting this pull request, I confirm that my contribution is made under the terms of the projects associated license.
1 parent 6212e27 commit 3b81de1

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

src/ALZ/Public/Remove-PlatformLandingZone.ps1

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -523,29 +523,41 @@ function Remove-PlatformLandingZone {
523523
function Invoke-PromptForConfirmation {
524524
param (
525525
[string]$Message,
526-
[string]$InitialConfirmationText,
527-
[string]$FinalConfirmationText = "YES I CONFIRM"
526+
[string]$FinalConfirmationText = "CONFIRM"
528527
)
529528

530529
Write-ToConsoleLog "$Message" -IsWarning
531-
Write-ToConsoleLog "If you wish to proceed, type '$InitialConfirmationText' to confirm." -IsWarning
530+
$randomString = (Get-RandomString -Length 6).ToUpper()
531+
Write-ToConsoleLog "If you wish to proceed, type '$randomString' to confirm." -IsWarning
532532
$confirmation = Read-Host "Enter the confirmation text"
533-
if ($confirmation -ne $InitialConfirmationText) {
534-
Write-ToConsoleLog "Confirmation not received. Exiting without making any changes." -IsError
533+
$confirmation = $confirmation.ToUpper().Replace("'","").Replace([System.Environment]::NewLine, "").Trim()
534+
if ($confirmation -ne $randomString.ToUpper()) {
535+
Write-ToConsoleLog "Confirmation text did not match the required input. Exiting without making any changes." -IsError
535536
return $false
536537
}
537538
Write-ToConsoleLog "Initial confirmation received." -IsSuccess
538539
Write-ToConsoleLog "This operation is permanent and cannot be reversed!" -IsWarning
539540
Write-ToConsoleLog "Are you sure you want to proceed? Type '$FinalConfirmationText' to perform the highly destructive operation..." -IsWarning
540541
$confirmation = Read-Host "Enter the final confirmation text"
541-
if ($confirmation -ne $FinalConfirmationText) {
542-
Write-ToConsoleLog "Final confirmation not received. Exiting without making any changes." -IsError
542+
$confirmation = $confirmation.ToUpper().Replace("'","").Replace([System.Environment]::NewLine, "").Trim()
543+
if ($confirmation -ne $FinalConfirmationText.ToUpper()) {
544+
Write-ToConsoleLog "Final confirmation did not match the required input. Exiting without making any changes." -IsError
543545
return $false
544546
}
545547
Write-ToConsoleLog "Final confirmation received. Proceeding with destructive operation..." -IsSuccess
546548
return $true
547549
}
548550

551+
function Get-RandomString {
552+
param (
553+
[int]$Length = 8
554+
)
555+
556+
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
557+
$string = -join ((1..$Length) | ForEach-Object { $chars[(Get-Random -Maximum $chars.Length)] })
558+
return $string
559+
}
560+
549561
function Remove-OrphanedRoleAssignmentsForScope {
550562
[CmdletBinding(SupportsShouldProcess = $true)]
551563
param (
@@ -974,18 +986,19 @@ function Remove-PlatformLandingZone {
974986
if(-not $BypassConfirmation) {
975987
Write-ToConsoleLog "The following Management Groups will be processed for removal:"
976988
$managementGroupsFound | ForEach-Object { Write-ToConsoleLog "Management Group: $($_.Name) ($($_.DisplayName))" -NoNewLine }
977-
$warningMessage = "ALL THE MANAGEMENT GROUP STRUCTURES ONE LEVEL BELOW THE LISTED MANAGEMENT GROUPS WILL BE PERMANENTLY DELETED"
978-
$confirmationText = "I CONFIRM I UNDERSTAND ALL THE MANAGEMENT GROUP STRUCTURES ONE LEVEL BELOW THE LISTED MANAGEMENT GROUPS WILL BE PERMANENTLY DELETED"
979-
if($DeleteTargetManagementGroups) {
980-
$warningMessage = "ALL THE LISTED MANAGEMENTS GROUPS AND THEIR CHILDREN WILL BE PERMANENTLY DELETED"
981-
$confirmationText = "I CONFIRM I UNDERSTAND ALL THE MANAGEMENT GROUPS AND THEIR CHILDREN WILL BE PERMANENTLY DELETED"
982-
}
983-
$continue = Invoke-PromptForConfirmation `
984-
-message $warningMessage `
985-
-initialConfirmationText $confirmationText
986-
if(-not $continue) {
987-
Write-ToConsoleLog "Exiting..."
988-
return
989+
990+
if($PlanMode) {
991+
Write-ToConsoleLog "Skipping confirmation for plan mode"
992+
} else {
993+
$warningMessage = "ALL THE MANAGEMENT GROUP STRUCTURES ONE LEVEL BELOW THE LISTED MANAGEMENT GROUPS WILL BE PERMANENTLY DELETED"
994+
if($DeleteTargetManagementGroups) {
995+
$warningMessage = "ALL THE LISTED MANAGEMENTS GROUPS AND THEIR CHILDREN WILL BE PERMANENTLY DELETED"
996+
}
997+
$continue = Invoke-PromptForConfirmation -message $warningMessage
998+
if(-not $continue) {
999+
Write-ToConsoleLog "Exiting..."
1000+
return
1001+
}
9891002
}
9901003
}
9911004

@@ -1204,12 +1217,15 @@ function Remove-PlatformLandingZone {
12041217
if(-not $BypassConfirmation) {
12051218
Write-ToConsoleLog "The following Subscriptions were provided or discovered during management group cleanup:"
12061219
$subscriptionsFinal | ForEach-Object { Write-ToConsoleLog "Name: $($_.Name), ID: $($_.Id)" -NoNewline }
1207-
$continue = Invoke-PromptForConfirmation `
1208-
-Message "ALL RESOURCE GROUPS IN THE LISTED SUBSCRIPTIONS WILL BE PERMANENTLY DELETED UNLESS THEY MATCH RETENTION PATTERNS" `
1209-
-InitialConfirmationText "I CONFIRM I UNDERSTAND ALL SELECTED RESOURCE GROUPS IN THE NAMED SUBSCRIPTIONS WILL BE PERMANENTLY DELETED"
1210-
if(-not $continue) {
1211-
Write-ToConsoleLog "Exiting..."
1212-
return
1220+
1221+
if($PlanMode) {
1222+
Write-ToConsoleLog "Skipping confirmation for plan mode"
1223+
} else {
1224+
$continue = Invoke-PromptForConfirmation -Message "ALL RESOURCE GROUPS IN THE LISTED SUBSCRIPTIONS WILL BE PERMANENTLY DELETED UNLESS THEY MATCH RETENTION PATTERNS"
1225+
if(-not $continue) {
1226+
Write-ToConsoleLog "Exiting..."
1227+
return
1228+
}
12131229
}
12141230
}
12151231
}

0 commit comments

Comments
 (0)