Skip to content

Commit 6b53882

Browse files
authored
Connect-DbaInstance, honor -Database parameter (#9585)
1 parent a074e4e commit 6b53882

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

public/Connect-DbaInstance.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,22 @@ function Connect-DbaInstance {
673673
$sqlConnectionInfo.Password = $csb.Password
674674
$null = $csb.Remove('Password')
675675
}
676+
# look for 'Initial Catalog' and 'Database' in the connection string
677+
$specifiedDatabase = $csb['Database']
678+
if ($specifiedDatabase -eq '') {
679+
$specifiedDatabase = $csb['Initial Catalog']
680+
}
681+
if ($Database -and $Database -ne $specifiedDatabase) {
682+
Write-Message -Level Debug -Message "Database specified in connection string '$specifiedDatabase' does not match Database parameter '$Database'. Database parameter will be used."
683+
# clear both, in order to not be overridden later by setting all AddtionalParameters
684+
if ($csb.ShouldSerialize('Database')) {
685+
$csb.Remove('Database')
686+
}
687+
if ($csb.ShouldSerialize('Initial Catalog')) {
688+
$csb.Remove('Initial Catalog')
689+
}
690+
$sqlConnectionInfo.DatabaseName = $Database
691+
}
676692

677693
# Add all remaining parts of the connection string as additional parameters.
678694
$sqlConnectionInfo.AdditionalParameters = $csb.ConnectionString

0 commit comments

Comments
 (0)