【问题标题】:Cannot connect to SQL Server from PowerShell with domain credentials无法使用域凭据从 PowerShell 连接到 SQL Server
【发布时间】:2013-03-15 05:36:51
【问题描述】:

我遇到无法使用域凭据连接到 SQL Server 的问题。使用 SA 凭据,它可以工作并且查询按预期运行。但是当我使用域凭据时,我得到一个错误。

这是脚本:

$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]

这是我在运行脚本时遇到的错误:

使用“1”参数调用“Fill”的异常:“发生网络相关或特定于实例的错误,同时 建立与 SQL Server 的连接。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

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

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

关于为什么我无法使用我的域凭据连接的任何想法?我关注了其他帖子,这些帖子提供了有关检查连接、防火墙等的建议。这一切都在工作中,脚本以 sa(本地)用户身份完美运行。只是不在域中..

【问题讨论】:

    标签: sql-server powershell windows-security


    【解决方案1】:

    您无法以这种方式使用域凭据连接。

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

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

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

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

    这将允许您的进程使用来自其他域的域凭据,这些凭据仅用于网络连接并在当时经过验证,同时仍将本地帐户用于其他事情。这可能会导致访问可能依赖于您的本地(或其他域?)帐户的其他网络资源时出现问题,如果您从同一进程建立不同的网络连接,我尚未完全测试 RUNAS /NETONLY 的工作原理。

    【讨论】:

      【解决方案2】:

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

      试试这样的 -

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

      【讨论】:

      • 谢谢。不幸的是,我需要它自动运行,所以虽然这个解决方案可能有效,但它不适合脚本的需求(如脚本所示 - 我已经需要脚本中的用户和密码)。
      • @MikeJ - 进行了编辑以将密码转换为安全字符串。这对你有用吗?
      • 这种技术行不通。无法在连接字符串中使用带有用户名/密码的集成 Windows 安全性。
      【解决方案3】:
      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 = "Server=myPC;Database=myDB;Integrated Security=True"

      【讨论】:

      • Integrated Security=True 将使用域凭据
      【解决方案4】:

      如果您有 SQL 凭据,请使用以下内容:

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

      如果您有域凭据,请使用以下内容:

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

      这适用于我的环境。当然之后你会这样做:

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

      【讨论】:

        【解决方案5】:

        如果您不担心安全性,您可以这样做(虽然不安全):

        #Set variables here
        
        $connectionString="server=$s;database=$sqlDbName;user id=$userName;password=$passWord";
        
        $sqlConnection=New-Object System.Data.SqlClient.SqlConnection($connectionString);
        

        它在 SQL Server 2012 中有效。但同样,安全。希望对某人有所帮助...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-10-30
          • 2011-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-22
          • 2016-07-02
          相关资源
          最近更新 更多