Closed andreasjordan closed 2 months ago
Maybe some other users can try to reproduce this:
$serverPooled = Connect-DbaInstance -SqlInstance SQL01 -SqlCredential $sqlAdminCredential
$serverPooled.ConnectionContext.CurrentDatabase # returns $null
$serverPooled.ConnectionContext.ProcessID # returns the spid without issung a query against the sql server
$serverPooled.ConnectionContext.CurrentDatabase # returns "master"
As we need ConnectionContext.CurrentDatabase insinde of Connect-DbaInstance to test if the database context changes, it should not be null.
How is it related to this issue: In the first case, the database context is copied as ConnectionContext.CurrentDatabase is not "master" as needed for Restore-DbaDatabase. Copying the context does not work for windows logins (as I use in the demo).
How to fix it: we should get the spid right after opening the connection. I'll open a PR for Connect-DbaInstance...
Getting the spid after opening the connection does not help as the pooled connection will be closed immediatly after. We have to get the spid to open a connection just before using ConnectionContext.CurrentDatabase.
The pull request #9308 seems to fix both parts of the issue, so it now works with pooled and non pooled connections.
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
Failure | Login failed for user 'labdom\User'.
Steps to Reproduce
Please confirm that you are running the most recent version of dbatools
Yes.
Other details or mentions
I will open a PR to fix the issue shortly.
What PowerShell host was used when producing this error
Windows PowerShell ISE (powershell_ise.exe)
PowerShell Host Version
PS5.1
SQL Server Edition and Build number
SQL 2022
.NET Framework Version
-