0

I am getting an error while connecting to a remote SQL Server from PowerShell with a domain account. I have admin rights to the system.

This is my script:

$SQLServer = "testsql"
$SQLDBName = "SQLInfo"
$uid ="FAREAST\testuser"
$ConnectionString="Server = $SQLServer; Database = $SQLDBName"
$pwd = "testpwd"
$dpwd = ConvertTo-SecureString -String $pwd -AsPlainText -Force
$dpwd.MakeReadOnly() 
$creds = New-Object System.Data.SqlClient.SqlCredential ($uid,$dpwd)
$con = New-Object System.Data.SqlClient.SqlConnection
$con.ConnectionString = $ConnectionString
$con.Credential = $creds
$con.Open() 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $con
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$DataSet.Tables[0] | out-file "D:\Eswar\data.txt"

I get the following error message:

Exception calling "Open" with "0" argument(s): "Login failed for user 'FAREAST\testUser'."
At line:12 char:2
+ $con.Open()
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Eshwar
  • 1
  • If I add trusted_connection=yes/Integrated Security=yes/SSPI it will ignore the credentials and login with current user account. I don't want to login with current user account – Eshwar Apr 02 '18 at 05:00
  • Please help me on this. Thanks in advance. – Eshwar Apr 02 '18 at 09:01

0 Answers0