Skip to content

Commit

Permalink
Connect-DbaInstance - Open connection to get CurrentDatabase (#9308)
Browse files Browse the repository at this point in the history
Co-authored-by: Chrissy LeMaire <[email protected]>
  • Loading branch information
andreasjordan and potatoqualitee authored Apr 12, 2024
1 parent 1f06a1c commit eacee59
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions public/Connect-DbaInstance.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -595,9 +595,17 @@ function Connect-DbaInstance {
# Currently only if we have a different Database or have to switch to a NonPooledConnection or using a specific StatementTimeout or using ApplicationIntent
# We do not test for SqlCredential as this would change the behavior compared to the legacy code path
$copyContext = $false
if ($Database -and $inputObject.ConnectionContext.CurrentDatabase -ne $Database) {
Write-Message -Level Verbose -Message "Database provided. Does not match ConnectionContext.CurrentDatabase, copying ConnectionContext and setting the CurrentDatabase"
$copyContext = $true
if ($Database) {
Write-Message -Level Debug -Message "Database [$Database] provided."
if (-not $inputObject.ConnectionContext.CurrentDatabase) {
Write-Message -Level Debug -Message "ConnectionContext.CurrentDatabase is empty, so connection will be opened to get the value"
$inputObject.ConnectionContext.Connect()
Write-Message -Level Debug -Message "ConnectionContext.CurrentDatabase is now [$($inputObject.ConnectionContext.CurrentDatabase)]"
}
if ($inputObject.ConnectionContext.CurrentDatabase -ne $Database) {
Write-Message -Level Verbose -Message "Database [$Database] provided. Does not match ConnectionContext.CurrentDatabase [$($inputObject.ConnectionContext.CurrentDatabase)], copying ConnectionContext and setting the CurrentDatabase"
$copyContext = $true
}
}
if ($ApplicationIntent -and $inputObject.ConnectionContext.ApplicationIntent -ne $ApplicationIntent) {
Write-Message -Level Verbose -Message "ApplicationIntent provided. Does not match ConnectionContext.ApplicationIntent, copying ConnectionContext and setting the ApplicationIntent"
Expand Down

0 comments on commit eacee59

Please sign in to comment.