diff --git a/public/Connect-DbaInstance.ps1 b/public/Connect-DbaInstance.ps1 index 368c8d6fdc..e8e4697123 100644 --- a/public/Connect-DbaInstance.ps1 +++ b/public/Connect-DbaInstance.ps1 @@ -639,10 +639,13 @@ function Connect-DbaInstance { if ($Database) { # Save StatementTimeout because it might be reset on GetDatabaseConnection $savedStatementTimeout = $connContext.StatementTimeout - $connContext = $connContext.GetDatabaseConnection($Database) + $connContext = $connContext.GetDatabaseConnection($Database, $false) $connContext.StatementTimeout = $savedStatementTimeout } $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $connContext + if ($Database -and $server.ConnectionContext.CurrentDatabase -ne $Database) { + Write-Message -Level Warning -Message "Changing connection context to database $Database was not successful. Current database is $($server.ConnectionContext.CurrentDatabase). Please open an issue on https://github.com/dataplat/dbatools/issues." + } } else { $server = $inputObject } diff --git a/tests/Connect-DbaInstance.Tests.ps1 b/tests/Connect-DbaInstance.Tests.ps1 index 7c066e83db..3f849bf67e 100644 --- a/tests/Connect-DbaInstance.Tests.ps1 +++ b/tests/Connect-DbaInstance.Tests.ps1 @@ -206,6 +206,13 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" { $serverClone.ConnectionContext.ServerInstance | Should -Match '^ADMIN:' $serverClone | Disconnect-DbaInstance } + + It "clones when using Backup-DabInstace" { + $server = Connect-DbaInstance -SqlInstance $script:instance1 -Database tempdb + $null = Backup-DbaDatabase -SqlInstance $server -Database msdb + $null = Backup-DbaDatabase -SqlInstance $server -Database msdb -WarningVariable warn + $warn | Should -BeNullOrEmpty + } } Context "multiple connections are properly made using strings" {