Cannot connect to SQL Server from PowerShell with domain credentials

邮差的信 提交于 2019-12-01 19:24:02
Thomas

If you have SQL Credentials use something like this:

$ConnectionString = server=*serverName*;database=*databaseName*;user id=*userName*;password=*passWord*;

If you have Domain Credentials use something like this:

$ConnectionString = "server=*serverName*;database=*databaseName*;user id=*domain\username*;password=*passWord*;trusted_connection=true;"

This works in my environment. Of course after that you do:

$Connection = New-Object System.Data.SQLClient.SQLConnection($ConnectionString)
Cade Roux

You cannot connect with domain credentials THAT WAY.

Domain credentials are present in the process making the connection. The account using in the process (or if using the NETWORK ONLY option in RUNAS) will be used to make the connection to SQL Server using integrated security.

Of course, because your process CANNOT actually run as a user on that domain because your computer is not in a trust relationship with that domain, I recommend you look at using RUNAS /NETONLY.

This will prompt for a password, and so you may have to write your own replacement for it which uses the CreateProcessWithLogonW API instead.

https://stackoverflow.com/a/758805/18255

This will allow your process to use domain credentials from the other domain which are only used on network connections and verified at that time, while still using the local account for other things. This MAY cause problems getting to other network resources which might rely on your local (or other domain?) account, I haven't fully tested how RUNAS /NETONLY works if you make DIFFERENT network connections from the same process.

Function SQL_CONN1 ($Query) {
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 
$SqlConnection.ConnectionString = "Server=myPC;Database=myDB;Integrated Security=True" 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
$SqlCmd.Connection = $SqlConnection 
$SqlCmd.CommandText = $Query 
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
$SqlAdapter.SelectCommand = $SqlCmd 
$DataSet = New-Object System.Data.DataSet 
$a=$SqlAdapter.Fill($DataSet) 
$SqlConnection.Close() 
$DataSet.Tables[0]  }

SQL_CONN1 "Select * from [MyTable]"

Try $SqlConnection.ConnectionString = "Server=myPC;Database=myDB;Integrated Security=True"

If you are not worried about security you can do it like this (not secure, though):

#Set variables here

$connectionString="server=$s;database=$sqlDbName;user id=$userName;password=$passWord";

$sqlConnection=New-Object System.Data.SqlClient.SqlConnection($connectionString);

It works in SQL Server 2012. But, again, not secure. Hope that helps someone...

You cannot pass username and password as string.

Try something like this -

#$cred = Get-Credential
# this will prompt for username and password, fill them in
# use this in your connection string
$secpwd = ConvertTo-SecureString $password -AsPlainText -Force

$SqlConnection.ConnectionString = 'Server=$SQLServer; Database=$SQLDBName; User ID=$username; Password=$secpwd;'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!