Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to SQL Server Database from PowerShell

I have looked around online for a while now and found many similar problems but for some reason I can't seem to get this working.

I am just trying to connect to a SQL server database and output the query results to a file - See PowerShell script below. What I am uncertain about is how to integrate the User ID and Password into the connection string.

$SQLServer = "aaaa.database.windows.net" $SQLDBName = "Database" $uid ="john" $pwd = "pwd123" $SqlQuery = "SELECT * from table;" $SqlConnection = New-Object System.Data.SqlClient.SqlConnection $SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True; User ID = $uid; Password = $pwd;" $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)  $DataSet.Tables[0] | out-file "C:\Scripts\xxxx.csv" 

The following error message is received:

Exception calling "Fill" with "1" argument(s): "Windows logins are not supported in this version of SQL Server."

like image 631
Johnathan Avatar asked Sep 05 '14 09:09

Johnathan


People also ask

How do I connect to a SQL Server database?

Connect to a SQL Server instanceStart SQL Server Management Studio. The first time you run SSMS, the Connect to Server window opens. If it doesn't open, you can open it manually by selecting Object Explorer > Connect > Database Engine. For Server type, select Database Engine (usually the default option).

How do I connect to SQL Server using Windows authentication in PowerShell?

By default, the SQL Server PowerShell components use Windows Authentication when connecting to an instance of the Database Engine. You can use SQL Server Authentication by either defining a PowerShell virtual drive, or by specifying the -Username and -Password parameters for Invoke-Sqlcmd.

How do I connect to SQL Server using PowerShell?

Use Win + R , on your Windows computer, and type PowerShell to launch a new Windows PowerShell session. SQL Server provides a PowerShell module named SqlServer. You can use the SqlServer module to import the SQL Server components (SQL Server provider and cmdlets) into a PowerShell environment or script.


2 Answers

Integrated Security and User ID \ Password authentication are mutually exclusive. To connect to SQL Server as the user running the code, remove User ID and Password from your connection string:

$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;" 

To connect with specific credentials, remove Integrated Security:

$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; User ID = $uid; Password = $pwd;" 
like image 177
Aaron Jensen Avatar answered Sep 23 '22 12:09

Aaron Jensen


Change Integrated security to false in the connection string.

You can check/verify this by opening up the SQL management studio with the username/password you have and see if you can connect/open the database from there. NOTE! Could be a firewall issue as well.

like image 43
ojk Avatar answered Sep 24 '22 12:09

ojk