Skip to content

Issue using Connect-DbaInstance using Windows credentials specified in PSCredential #9865

@ZenoArrow

Description

@ZenoArrow

Verified issue does not already exist?

I have searched and found no existing issue

What error did you receive?

Take a look at the following code:

Clear-Host

Import-Module -Name dbatools

# Used to get around error "The certificate chain was issued by an authority that is not trusted"
Set-DbatoolsInsecureConnection -SessionOnly

$userName = 'DOMAIN\user'

$loginCredentials = Get-Credential -Message 'Test' -UserName $userName

$sqlServerHostname = 'SERVERNAME.domain.local'

# Succeeds (gets disk space for each of the local drives on the server)
Get-DbaDiskSpace -ComputerName $sqlServerHostname -Credential $loginCredentials

# Fails ("The user name or password is incorrect")
Connect-DbaInstance -SqlInstance $sqlServerHostname -SqlCredential $loginCredentials

I have altered the $userName and $sqlServerHostname values to hide the real values used, but the script otherwise remains unchanged. The PSCredential specified in $loginCredentials was clearly entered correctly, as otherwise Get-DbaDiskSpace would not work, and I can login to the SQL Server instance using SSMS with the same username/password, so it's not a permission issue. If I run the following in a PowerShell session running on the server specified in $sqlServerHostname (using the username/password tested in the script to login to the box using RDP), I can login:

Set-DbatoolsInsecureConnection -SessionOnly

$sqlServerHostname = 'SERVERNAME.domain.local'

Connect-DbaInstance -SqlInstance $sqlServerHostname

It's not clear to me why the behaviour would be different when trying to login to a SQL Server instance via a PSCredential.

Steps to Reproduce

Clear-Host

Import-Module -Name dbatools

# Used to get around error "The certificate chain was issued by an authority that is not trusted"
Set-DbatoolsInsecureConnection -SessionOnly

$userName = 'DOMAIN\user'

$loginCredentials = Get-Credential -Message 'Test' -UserName $userName

$sqlServerHostname = 'SERVERNAME.domain.local'

# Succeeds (gets disk space for each of the local drives on the server)
Get-DbaDiskSpace -ComputerName $sqlServerHostname -Credential $loginCredentials

# Fails ("The user name or password is incorrect")
Connect-DbaInstance -SqlInstance $sqlServerHostname -SqlCredential $loginCredentials

Please confirm that you are running the most recent version of dbatools

2.7.6

Other details or mentions

No response

What PowerShell host was used when producing this error

Windows PowerShell ISE (powershell_ise.exe)

PowerShell Host Version

Name Value


PSVersion 5.1.20348.4163
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.20348.4163
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

SQL Server Edition and Build number

Microsoft SQL Server 2022 (RTM-CU21) (KB5065865) - 16.0.4215.2 (X64) Aug 11 2025 13:24:21 Copyright (C) 2022 Microsoft Corporation Standard Edition (64-bit) on Windows Server 2022 Datacenter 10.0 (Build 20348: ) (Hypervisor)

.NET Framework Version

.NET Framework 4.8.4795.0

Does .NET Framework 4.6 need to be installed if a later 4.x version is present?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugs lifetriage requiredNew issue that has not been reviewed by maintainers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions