title: Cannot connect to SQL Server from PowerShell with domain credentials
body: <p>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.</p>
  
  <p>Here is the script:</p>
  
  <pre><code>$SQLServer = 'server.domain.com'
  $SQLDBName = 'DBname'
  $username  = "DOMAIN\user"
  $password  = "password"
  
  $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
  
  #--&gt; With SA, it works, with DOMAIN creds, it does not
  
  #$SqlConnection.ConnectionString = "Server=$SQLServer; Database=$SQLDBName; User ID=sa; Password=SApassword;"
  $SqlConnection.ConnectionString = "Server=$SQLServer; Database=$SQLDBName; User ID=$username; Password=$password;"
  
  $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
  $SqlCmd.CommandText = 'select count (*) from my.table'
  $SqlCmd.Connection = $SqlConnection
  $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  $SqlAdapter.SelectCommand = $SqlCmd
  $DataSet = New-Object System.Data.DataSet
  $SqlAdapter.Fill($DataSet)
  $SqlConnection.Close()
  $DataSet.Tables[0]
  </code></pre>
  
  <p>And here is the error I get when I run the script:</p>
  
  <blockquote>
    <p>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)</p>
    
    <p>At C:\scripts\PowerShell\myscript.ps1:28 char:1<br>
    + $SqlAdapter.Fill($DataSet)<br>
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException<br>
    + FullyQualifiedErrorId : SqlException</p>
  </blockquote>
  
  <p>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).</p>
  
  <p>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..</p>