Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect-DbaInstance, honor -Database parameter #9585

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,22 @@ function Connect-DbaInstance {
$sqlConnectionInfo.Password = $csb.Password
$null = $csb.Remove('Password')
}
# look for 'Initial Catalog' and 'Database' in the connection string
$specifiedDatabase = $csb['Database']
if ($specifiedDatabase -eq '') {
$specifiedDatabase = $csb['Initial Catalog']
}
if ($Database -and $Database -ne $specifiedDatabase) {
Write-Message -Level Debug -Message "Database specified in connection string '$specifiedDatabase' does not match Database parameter '$Database'. Database parameter will be used."
# clear both, in order to not be overridden later by setting all AddtionalParameters
if ($csb.ShouldSerialize('Database')) {
$csb.Remove('Database')
}
if ($csb.ShouldSerialize('Initial Catalog')) {
$csb.Remove('Initial Catalog')
}
$sqlConnectionInfo.DatabaseName = $Database
}

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