Skip to content

Commit dd58a4e

Browse files
Export-DbaInstance: Enhance handling of dedicated admin connections (#10173)
1 parent ffb91a1 commit dd58a4e

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

private/testing/Get-TestConfig.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ function Get-TestConfig {
2525
Write-Host -Object "Get-TestConfig: Setting up test configuration for AppVeyor" -ForegroundColor DarkGreen
2626

2727
if ($env:InstanceSingle) {
28-
$config['InstanceSingle'] = "$(hostname)\$($env:InstanceSingle)"
28+
$config['InstanceSingle'] = "$env:COMPUTERNAME\$env:InstanceSingle"
2929
}
3030
if ($env:InstanceMulti1) {
31-
$config['InstanceMulti1'] = "$(hostname)\$($env:InstanceMulti1)"
31+
$config['InstanceMulti1'] = "$env:COMPUTERNAME\$env:InstanceMulti1"
3232
}
3333
if ($env:InstanceMulti2) {
34-
$config['InstanceMulti2'] = "$(hostname)\$($env:InstanceMulti2)"
34+
$config['InstanceMulti2'] = "$env:COMPUTERNAME\$env:InstanceMulti2"
3535
}
3636
if ($env:InstanceCopy1) {
37-
$config['InstanceCopy1'] = "$(hostname)\$($env:InstanceCopy1)"
37+
$config['InstanceCopy1'] = "$env:COMPUTERNAME\$env:InstanceCopy1"
3838
}
3939
if ($env:InstanceCopy2) {
40-
$config['InstanceCopy2'] = "$(hostname)\$($env:InstanceCopy2)"
40+
$config['InstanceCopy2'] = "$env:COMPUTERNAME\$env:InstanceCopy2"
4141
}
4242
if ($env:InstanceHadr) {
43-
$config['InstanceHadr'] = "$(hostname)\$($env:InstanceHadr)"
43+
$config['InstanceHadr'] = "$env:COMPUTERNAME\$env:InstanceHadr"
4444
}
4545
if ($env:InstanceRestart) {
46-
$config['InstanceRestart'] = "$(hostname)\$($env:InstanceRestart)"
46+
$config['InstanceRestart'] = "$env:COMPUTERNAME\$env:InstanceRestart"
4747
}
4848

4949
$config['SQLUserName'] = $null # placeholders for -SqlCredential testing

public/Export-DbaInstance.ps1

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,39 @@ function Export-DbaInstance {
217217
foreach ($instance in $SqlInstance) {
218218
$stepCounter = 0
219219
try {
220-
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
220+
# Do we need a dedicated admin connection for password retrieval?
221+
# If not both are excluded, we do
222+
$dacNeeded = $Exclude -notcontains 'Credentials' -or $Exclude -notcontains 'LinkedServers'
223+
# If passwords are excluded, we don't need a DAC
224+
if ($ExcludePassword) { $dacNeeded = $false }
225+
226+
# Do we have a dedicated admin connection already?
227+
$dacConnected = $instance.Type -eq 'Server' -and $instance.InputObject.Name -match '^ADMIN:'
228+
229+
$dacOpened = $false
230+
if ($dacNeeded) {
231+
if ($dacConnected) {
232+
Write-Message -Level Verbose -Message "Reusing dedicated admin connection for password retrieval."
233+
$server = $instance.InputObject
234+
} else {
235+
Write-Message -Level Verbose -Message "Opening dedicated admin connection for password retrieval."
236+
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10 -DedicatedAdminConnection -WarningAction SilentlyContinue
237+
$dacOpened = $true
238+
}
239+
} else {
240+
Write-Message -Level Verbose -Message "Opening or reusing normal connection because passwords are excluded."
241+
$server = Connect-DbaInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 10
242+
}
221243
} catch {
222244
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
223245
}
224246

225247
if ($Force) {
226248
# when the caller requests to overwrite existing scripts we won't add the dynamic timestamp to the folder name, so that a pre-existing location can be overwritten.
227-
$exportPath = Join-DbaPath -Path $Path -Child "$($server.name.replace('\', '$'))"
249+
$exportPath = Join-DbaPath -Path $Path -Child "$($server.DomainInstanceName.replace('\', '$'))"
228250
} else {
229251
$timeNow = (Get-Date -UFormat (Get-DbatoolsConfigValue -FullName 'formatting.uformat'))
230-
$exportPath = Join-DbaPath -Path $Path -Child "$($server.name.replace('\', '$'))-$timeNow"
252+
$exportPath = Join-DbaPath -Path $Path -Child "$($server.DomainInstanceName.replace('\', '$'))-$timeNow"
231253
}
232254

233255
# Ensure the export dir exists.
@@ -264,7 +286,7 @@ function Export-DbaInstance {
264286
if ($Exclude -notcontains 'Credentials') {
265287
Write-Message -Level Verbose -Message "Exporting SQL credentials"
266288
Write-ProgressHelper -StepNumber ($stepCounter++) -Message "Exporting SQL credentials"
267-
$null = Export-DbaCredential -SqlInstance $server -FilePath "$exportPath\credentials.sql" -ExcludePassword:$ExcludePassword
289+
$null = Export-DbaCredential -SqlInstance $server -Credential $Credential -FilePath "$exportPath\credentials.sql" -ExcludePassword:$ExcludePassword
268290
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\credentials.sql"
269291
}
270292

@@ -447,6 +469,9 @@ function Export-DbaInstance {
447469
Get-ChildItem -ErrorAction Ignore -Path "$exportPath\oledbprovider.sql"
448470
}
449471

472+
if ($dacOpened) {
473+
$null = $server | Disconnect-DbaInstance -WhatIf:$false
474+
}
450475

451476
Write-Progress -Activity "Performing Instance Export for $instance" -Completed
452477
}

tests/Export-DbaInstance.Tests.ps1

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Describe $CommandName -Tag IntegrationTests {
6060

6161
# registered server and group
6262
$testServer = $TestConfig.InstanceSingle
63-
$server = Connect-DbaInstance -SqlInstance $testServer
6463
$srvName = "dbatoolsci-server1"
6564
$group = "dbatoolsci-group1"
6665
$regSrvName = "dbatoolsci-server12"
@@ -260,6 +259,22 @@ Describe $CommandName -Tag IntegrationTests {
260259
$results.Length | Should -BeGreaterThan 0
261260
}
262261

262+
It "Export credentials without passwords" {
263+
$results = Export-DbaInstance -SqlInstance $testServer -Path $exportDir -Exclude 'AgentServer', 'Audits', 'AvailabilityGroups', 'BackupDevices', 'CentralManagementServer', 'CustomErrors', 'DatabaseMail', 'Databases', 'Endpoints', 'ExtendedEvents', 'LinkedServers', 'Logins', 'PolicyManagement', 'ReplicationSettings', 'ResourceGovernor', 'ServerAuditSpecifications', 'ServerRoles', 'SpConfigure', 'SysDbUserObjects', 'SystemTriggers', 'OleDbProvider' -ExcludePassword
264+
265+
$results.FullName | Should -Exist
266+
$results.Length | Should -BeGreaterThan 0
267+
}
268+
269+
It "Export credentials with preopend dac" {
270+
$dac = Connect-DbaInstance -SqlInstance $testServer -DedicatedAdminConnection
271+
$results = Export-DbaInstance -SqlInstance $dac -Path $exportDir -Exclude 'AgentServer', 'Audits', 'AvailabilityGroups', 'BackupDevices', 'CentralManagementServer', 'CustomErrors', 'DatabaseMail', 'Databases', 'Endpoints', 'ExtendedEvents', 'LinkedServers', 'Logins', 'PolicyManagement', 'ReplicationSettings', 'ResourceGovernor', 'ServerAuditSpecifications', 'ServerRoles', 'SpConfigure', 'SysDbUserObjects', 'SystemTriggers', 'OleDbProvider' -ExcludePassword
272+
$null = $dac | Disconnect-DbaInstance
273+
274+
$results.FullName | Should -Exist
275+
$results.Length | Should -BeGreaterThan 0
276+
}
277+
263278
It "Export logins" {
264279
$results = Export-DbaInstance -SqlInstance $testServer -Path $exportDir -Exclude 'AgentServer', 'Audits', 'AvailabilityGroups', 'BackupDevices', 'CentralManagementServer', 'Credentials', 'CustomErrors', 'DatabaseMail', 'Databases', 'Endpoints', 'ExtendedEvents', 'LinkedServers', 'PolicyManagement', 'ReplicationSettings', 'ResourceGovernor', 'ServerAuditSpecifications', 'ServerRoles', 'SpConfigure', 'SysDbUserObjects', 'SystemTriggers', 'OleDbProvider'
265280

0 commit comments

Comments
 (0)