Skip to content

Commit 14a287e

Browse files
Connect-DbaInstance: Use localhost for dedicated admin connections to avoid multiple IP resolution on multi-homed servers (#10199)
1 parent a0833cc commit 14a287e

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

public/Connect-DbaInstance.ps1

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,17 @@ function Connect-DbaInstance {
640640

641641
if ($DedicatedAdminConnection -and $serverName) {
642642
Write-Message -Level Debug -Message "Parameter DedicatedAdminConnection is used, so serverName will be changed and NonPooledConnection will be set."
643-
$serverName = 'ADMIN:' + $serverName
643+
if ($instance.IsLocalHost) {
644+
# Use localhost to avoid multiple IP resolution on multi-homed servers (issue #10151)
645+
if ($instance.InstanceName -ne 'MSSQLSERVER') {
646+
$serverName = "ADMIN:localhost\$($instance.InstanceName)"
647+
} else {
648+
$serverName = "ADMIN:localhost"
649+
}
650+
Write-Message -Level Debug -Message "IsLocalHost is true, using '$serverName' for DAC to avoid multi-IP resolution."
651+
} else {
652+
$serverName = "ADMIN:$serverName"
653+
}
644654
$NonPooledConnection = $true
645655
}
646656

@@ -704,7 +714,16 @@ function Connect-DbaInstance {
704714
$connContext.StatementTimeout = $StatementTimeout
705715
}
706716
if ($DedicatedAdminConnection -and $inputObject.ConnectionContext.ServerInstance -notmatch '^ADMIN:') {
707-
$connContext.ServerInstance = 'ADMIN:' + $connContext.ServerInstance
717+
if ($instance.IsLocalHost) {
718+
# Use localhost to avoid multiple IP resolution on multi-homed servers (issue #10151)
719+
if ($instance.InstanceName -ne 'MSSQLSERVER') {
720+
$connContext.ServerInstance = "ADMIN:localhost\$($instance.InstanceName)"
721+
} else {
722+
$connContext.ServerInstance = "ADMIN:localhost"
723+
}
724+
} else {
725+
$connContext.ServerInstance = 'ADMIN:' + $connContext.ServerInstance
726+
}
708727
$connContext.NonPooledConnection = $true
709728
}
710729
if ($Database) {

tests/Connect-DbaInstance.Tests.ps1

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,30 @@ Describe $CommandName -Tag IntegrationTests {
281281
}
282282

283283
Context "multiple dedicated admin connections are properly made using strings" {
284-
# This might fail if a parallel test uses DAC - how can we ensure that this is the only test that is run?
285284
It "opens and closes the connections" {
285+
$instance1 = [DbaInstanceParameter]$TestConfig.InstanceMulti1
286+
if ($instance1.IsLocalHost) {
287+
if ($instance1.InstanceName -ne 'MSSQLSERVER') {
288+
$name1 = "ADMIN:localhost\$($instance1.InstanceName)"
289+
} else {
290+
$name1 = "ADMIN:localhost"
291+
}
292+
} else {
293+
$name1 = 'ADMIN:' + $instance1.FullName
294+
}
295+
$instance2 = [DbaInstanceParameter]$TestConfig.InstanceMulti2
296+
if ($instance2.IsLocalHost) {
297+
if ($instance2.InstanceName -ne 'MSSQLSERVER') {
298+
$name2 = "ADMIN:localhost\$($instance2.InstanceName)"
299+
} else {
300+
$name2 = "ADMIN:localhost"
301+
}
302+
} else {
303+
$name2 = 'ADMIN:' + $instance2.FullName
304+
}
286305
$server = Connect-DbaInstance -SqlInstance $TestConfig.InstanceMulti1, $TestConfig.InstanceMulti2 -DedicatedAdminConnection
287-
$server[0].Name | Should -Be "ADMIN:$($TestConfig.InstanceMulti1)"
288-
$server[1].Name | Should -Be "ADMIN:$($TestConfig.InstanceMulti2)"
306+
$server[0].Name | Should -Be $name1
307+
$server[1].Name | Should -Be $name2
289308
$null = $server | Disconnect-DbaInstance
290309
# DAC is not reopened in the background
291310
Start-Sleep -Seconds 10

0 commit comments

Comments
 (0)