The SQL server object created by this cmdlet uses the wrong credentials
Verified issue does not already exist?
I have searched and found no existing issue
What error did you receive?
In a script, running as LOCAL SYSTEM account, I am creating an SQL server object using this code:
## Create the SQL connection.
$CmdletParams = @{
SqlInstance = 'sqlserver01'
SqlCredential = $ServiceCredentials
TrustServerCertificate = $true
}
$SqlConnection = Connect-DbaInstance @CmdletParams
After that, I use the object in the Invoke-DbaQuery cmdlet:
$CmdletParams = @{
SqlInstance = $SqlConnection
Database = 'DatabaseName'
Query = 'TRUNCATE TABLE Table_Name'
}
Invoke-DbaQuery @CmdletParams
But, in the logs, I get the following error:
Looks like both cmdlets try to connect using the machine account (which is associated with the SYSTEM account) instead of using the credentials defined in the $SqlConnection = Connect-DbaInstance @CmdletParams. Why is that happening?
Steps to Reproduce
Steps to repro provided above.
Please confirm that you are running the most recent version of dbatools
I confirm.
Other details or mentions
No response
What PowerShell host was used when producing this error
Windows PowerShell (powershell.exe)
PowerShell Host Version
5.1
SQL Server Edition and Build number
Unknown
.NET Framework Version
PSChildName Version
v2.0.50727 2.0.50727.4927 v3.0 3.0.30729.4926 Windows Communication Foundation 3.0.4506.4926 Windows Presentation Foundation 3.0.6920.4902 v3.5 3.5.30729.4926 Client 4.8.09032 Full 4.8.09032 Client 4.0.0.0
I added the credentials to each cmdlet and now both are working fine. I think my assumption that the SQL object will also contain the credentials was wrong.
Unfortunately, after adding the credential directly into the cmdlet, I still get the same error.
I don't really know how it worked the first time...
But now I have the same error in the logs as the one posted above. And the code looks like this:
Just wanted to confirm I'm seeing the same issue on 2.1.23
FYI I can it to work if I add "-NonPooledConnection" to the connect-dbainstance cmdlet
linked/relative to https://github.com/dataplat/dbatools/issues/9579