Skip to content

Commit 29182e4

Browse files
New-DbaAvailabilityGroup - Let the validation guards reach their own return (#10642)
1 parent 736007e commit 29182e4

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

public/New-DbaAvailabilityGroup.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,28 +534,30 @@ function New-DbaAvailabilityGroup {
534534
if (($SharedPath)) {
535535
if (-not (Test-DbaPath -SqlInstance $Primary -SqlCredential $PrimarySqlCredential -Path $SharedPath)) {
536536
Write-Progress -Activity "Adding new availability group" -Completed
537-
Stop-Function -Continue -Message "Cannot access $SharedPath from $Primary"
537+
# No -Continue on these guards: no loop encloses them, so the continue escaped the
538+
# command before the return below ever ran and ate an iteration of the caller's loop.
539+
Stop-Function -Message "Cannot access $SharedPath from $Primary"
538540
return
539541
}
540542
}
541543

542544
if ($Database -and -not $UseLastBackup -and -not $SharedPath -and $Secondary -and $SeedingMode -ne 'Automatic') {
543545
Write-Progress -Activity "Adding new availability group" -Completed
544-
Stop-Function -Continue -Message "You must specify a SharedPath when adding databases to a manually seeded availability group"
546+
Stop-Function -Message "You must specify a SharedPath when adding databases to a manually seeded availability group"
545547
return
546548
}
547549

548550
if ($server.HostPlatform -eq "Linux") {
549551
# New to SQL Server 2017 (14.x) is the introduction of a cluster type for AGs. For Linux, there are two valid values: External and None.
550552
if ($ClusterType -notin "External", "None") {
551553
Write-Progress -Activity "Adding new availability group" -Completed
552-
Stop-Function -Continue -Message "Linux only supports ClusterType of External or None"
554+
Stop-Function -Message "Linux only supports ClusterType of External or None"
553555
return
554556
}
555557
# Microsoft Distributed Transaction Coordinator (DTC) is not supported under Linux in SQL Server 2017
556558
if ($DtcSupport) {
557559
Write-Progress -Activity "Adding new availability group" -Completed
558-
Stop-Function -Continue -Message "Microsoft Distributed Transaction Coordinator (DTC) is not supported under Linux"
560+
Stop-Function -Message "Microsoft Distributed Transaction Coordinator (DTC) is not supported under Linux"
559561
return
560562
}
561563
}

tests/New-DbaAvailabilityGroup.Tests.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,29 @@ Describe $CommandName -Tag IntegrationTests {
102102
$PSDefaultParameterValues.Remove("*-Dba*:EnableException")
103103
}
104104

105+
Context "When SharedPath is not accessible" {
106+
It "Warns without eating an iteration of the caller's loop" {
107+
# The validation guards used to run Stop-Function -Continue without an enclosing loop -
108+
# the continue escaped the command before its own return statement ran and consumed an
109+
# iteration of this very loop, so the counter fell short (#10638).
110+
$loopCount = 0
111+
foreach ($i in 1..3) {
112+
$splatBadPath = @{
113+
Primary = $TestConfig.InstanceHadr
114+
Name = "dbatoolsci_agbadpath"
115+
ClusterType = "None"
116+
FailoverMode = "Manual"
117+
SharedPath = "Q:\dbatoolsci\does\not\exist"
118+
WarningAction = "SilentlyContinue"
119+
}
120+
$null = New-DbaAvailabilityGroup @splatBadPath
121+
$loopCount++
122+
}
123+
$loopCount | Should -Be 3
124+
$WarnVar | Should -BeLike "*Cannot access*"
125+
}
126+
}
127+
105128
Context "When creating availability groups" {
106129
It "returns an ag with a db named" {
107130
$splatAg = @{

0 commit comments

Comments
 (0)