2

I wrote the following script with the original connection names and credentials.

$SQLServer = "servername" 
$SQLDBName = "dbname"
$SqlQuery = "select * from tbl_Employee "

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName;
User ID= xyz; Password= abc; trusted_connection=true;"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

After hitting enter I get the following error:

Exception calling "Fill" with "1" argument(s) :"Cannot generate SSPI context."
At line:1 char:17
+$SqlAdapter.Fill <<<< ($DataSet)
+CategoryInfo: NotSpecified: (:)[],MethodInvocationException
+FullyQualifiedErrorId: DotNetMethodException

I am trying to connect to the client's database using the given credentials but with the above code, its taking the windows credentials by default.

2

Connection String should instead read:

DRIVER=SQL Server;Server=$SQLServer;Database=$SQLDBName;UID=xyz;PWD=abc;

2

As you state you are trying to connect with specific credentials the problem is with your connection string.

As you use the following connection string:

Server = $SQLServer; Database = $SQLDBName;User ID= xyz; Password= abc; trusted_connection=true;

You will always be using Windows Authentication.

They key here is that you add

trusted_connection=true

Which will take precedence over the username and password combination.

Please see this blog post on MSDN Connectionstrings, mixing usernames and windows authentication. Who goes first?

Which clearly states

If “With Integrated Windows Authentication” is selected, this will takes precedence over the username/password in the connection string.

So the solution to your problem would be to remove the trusted_connection part

Server = $SQLServer; Database = $SQLDBName;User ID= xyz; Password= abc;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.