Cannot connect to SQL Server from PowerShell with domain credentials

Question:

I have a problem where I cannot connect to a SQL Server using domain credentials. Using the SA credentials it works and the query runs as expected. But when I use domain credentials, I get an error.

Here is the script:

And here is the error I get when I run the script:

Exception calling “Fill” with “1” argument(s): “A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

At C:\scripts\PowerShell\myscript.ps1:28 char:1
+ $SqlAdapter.Fill($DataSet)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException

I should point out the fact that I am connecting to a SQL Server from a computer which is NOT in the domain (therefore I cannot use any sort of authentication passthrough).

Any ideas as to why I cannot connect with my domain credentials? I’ve followed other posts which offer advice on checking connections, firewalls etc. That’s all in working order and the script runs perfectly as the sa (local) user. Just not on the domain..

Answer:

If you have SQL Credentials use something like this:

If you have Domain Credentials use something like this:

This works in my environment. Of course after that you do:

Source:

Cannot connect to SQL Server from PowerShell with domain credentials by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply