dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

The SQL server object created by this cmdlet uses the wrong credentials

Open andreiv3103 opened this issue 1 year ago • 4 comments

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:

Screenshot 2024-09-01 135451

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

andreiv3103 avatar Sep 01 '24 10:09 andreiv3103

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.

andreiv3103 avatar Sep 01 '24 11:09 andreiv3103

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: image

andreiv3103 avatar Sep 02 '24 07:09 andreiv3103

Just wanted to confirm I'm seeing the same issue on 2.1.23

jebbster88 avatar Sep 12 '24 08:09 jebbster88

FYI I can it to work if I add "-NonPooledConnection" to the connect-dbainstance cmdlet

jebbster88 avatar Sep 12 '24 09:09 jebbster88

linked/relative to https://github.com/dataplat/dbatools/issues/9579

niphlod avatar Feb 17 '25 21:02 niphlod