2013-03-25 54 views
2

我遇到了无法使用域凭据连接到SQL Server的问题。使用它的工作原理和查询按预期运行。但是,当我使用域凭据时,出现错误。无法使用域凭据从PowerShell连接到SQL Server

下面是脚本:

$SQLServer = 'server.domain.com' 
$SQLDBName = 'DBname' 
$username = "DOMAIN\user" 
$password = "password" 

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection 

#--> With SA, it works, with DOMAIN creds, it does not 

#$SqlConnection.ConnectionString = "Server=$SQLServer; Database=$SQLDBName; User ID=sa; Password=SApassword;" 
$SqlConnection.ConnectionString = "Server=$SQLServer; Database=$SQLDBName; User ID=$username; Password=$password;" 

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand 
$SqlCmd.CommandText = 'select count (*) from my.table' 
$SqlCmd.Connection = $SqlConnection 
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter 
$SqlAdapter.SelectCommand = $SqlCmd 
$DataSet = New-Object System.Data.DataSet 
$SqlAdapter.Fill($DataSet) 
$SqlConnection.Close() 
$DataSet.Tables[0] 

这里是我的错误,当我运行该脚本:

Exception calling "Fill" with "1" argument(s): "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

At C:\scripts\PowerShell\myscript.ps1:28 char:1
+ $SqlAdapter.Fill($DataSet)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SqlException

我要指出的是,我连接到SQL Server从不在域中的计算机(因此我不能使用任何类型的身份验证传递)。

关于为什么我无法连接到我的域凭据的任何想法?我跟着其他职位,提供有关检查连接,防火墙等建议。这一切都在工作顺序和脚本作为sa(本地)用户完美运行。只是不在域..

回答

3

你不能连接域的凭据这种方式。

域名凭证存在于进行连接的过程中。在进程中使用的帐户(或者如果在RUNAS中使用NETWORK ONLY选项)将用于使用集成安全性连接到SQL Server。

当然,因为您的进程实际上无法作为该域的用户运行,因为您的计算机与该域没有信任关系,所以我建议您查看使用RUNAS/NETONLY。

这将提示输入密码,因此您可能必须编写自己的替代品,而不是使用CreateProcessWithLogonW API。

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

这将使你的过程中使用从仅用于网络连接,并核实当时的其他域的域凭据,同时仍使用本地帐户的其他事情。这可能会导致其他网络资源可能依赖于您的本地(或其他域?)帐户,我还没有完全测试RUNAS/NETONLY如何从同一进程进行不同的网络连接。

-1

您无法将用户名和密码作为字符串传递。

尝试是这样的 -

#$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;' 
+0

感谢您的支持。不幸的是我需要它自动运行,所以虽然这个解决方案可能工作,但它不适合脚本的需求(如脚本指出 - 我已经在脚本中需要用户和密码)。 – 2013-03-25 16:42:21

+0

@MikeJ - 编辑将密码转换为securestring。这对你有用吗? – 2013-03-25 17:00:40

+0

这种技术不起作用。没有办法在连接字符串中使用集成的Windows安全性和用户名/密码。 – 2013-03-25 20:13:24

2
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]" 

尝试 $ SqlConnection.ConnectionString = “服务器= MYPC;数据库= MYDB;集成安全性=真”

+0

集成安全性= True将使用域凭据 – 2014-02-28 20:29:55

4

如果您有SQL凭证使用这样的事情:

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

如果你有Domain Credent使用类似这样的东西:

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

这适用于我的环境。当然,以后你做:

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

如果您不担心安全问题,你可以做这样的(不安全,虽然):

#Set variables here 

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

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

它可以在SQL Server 2012中但是,再次,不是安全。希望可以帮助别人......