title: SQL Connection From powershell
body: <p>I'm trying to connect my script to a Database.
  Previously I made it with:</p>
  
  <pre><code>psexec \\servername -accepteula sqlcmd.exe -U username -P password -S database -Q query
  </code></pre>
  
  <p>Now, I want to convert it into powershell code.
  I'm trying this:</p>
  
  <pre><code>Enter-PSSession servername
  $SQLCred = New-Object System.Data.SqlClient.SqlCredential("username","password")
  $Connection = New-Object System.Data.SqlClient.SqlConnection("Database",$SQLCred)
  $Connection.Open()
  </code></pre>
  
  <p>But I always get this error:</p>
  
  <pre><code>Exception calling "Open" with "0" argument(s): "Login failed for user 'username'."
  </code></pre>
  
  <p>Of course I'm sure the credentials are correct.
  Which could be the difference between the two modes of connection?</p>