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