我遇到了无法使用域凭据连接到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(本地)用户完美运行。只是不在域..
感谢您的支持。不幸的是我需要它自动运行,所以虽然这个解决方案可能工作,但它不适合脚本的需求(如脚本指出 - 我已经在脚本中需要用户和密码)。 – 2013-03-25 16:42:21
@MikeJ - 编辑将密码转换为securestring。这对你有用吗? – 2013-03-25 17:00:40
这种技术不起作用。没有办法在连接字符串中使用集成的Windows安全性和用户名/密码。 – 2013-03-25 20:13:24