Powershell "Invoke-Sqlcmd2" 未连接 Windows 帐户凭据
Powershell "Invoke-Sqlcmd2" not connecting with Windows Account credentials
我有一个脚本调用 Invoke-Sqlcmd2
$password = ConvertTo-SecureString 'xxxxxxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('domain\someuser', $password)
$resultListTable = Invoke-Sqlcmd2 -Credential $cred -query $query -ServerInstance "SERVER" -Database ZZZZ -As "DataTable"
失败并显示错误消息
Exception calling "Open" with "0" argument(s): "Login failed for user 'domain\someuser'."
我的 DBA 向我报告说他可以从服务器看到如下所示的错误:
Login failed for user 'domain\someuser' Reason: Attempting to use an NT account name with SQL Server Authentication.
Invoke-SqlCmd2 在 ( https://github.com/sqlcollaborative/Invoke-SqlCmd2/blob/master/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 ) 的文档表明支持 windows 和 sql 身份验证。 (我试过只使用 sql 凭据,效果很好,但出于商业原因,我需要使用我拥有的 windows 凭据)。而且我已经看到(在某个地方,现在找不到)只需在用户名中提供反斜杠就足以(大概是 System.Data.SqlClient.SQLConnection
对象)进行 select windows 身份验证.不幸的是,这不是我看到的行为。
调用 Windows 身份验证启动时,我还需要做些什么吗?
代码实际做的是:
if ($Credential) {
$CSBuilder["Trusted_Connection"] = $false
$CSBuilder["User ID"] = $Credential.UserName
$CSBuilder["Password"] = $Credential.GetNetworkCredential().Password
}
else {
$CSBuilder["Integrated Security"] = $true
}
(见source code)
...所以基本上,如果您提供凭据,那么它会被假定为 SQL 服务器登录,而不是 windows 登录(因为可信连接设置为 false)。
N.B。您天生就误解了 SQL 服务器的身份验证是如何工作的。 Windows 对 SQL 服务器的授权是基于 当前 登录用户隐式完成的。如果 integrated/trusted 身份验证在连接字符串中设置为打开,则 SQL 将自动使用 windows 用户建立连接的上下文来确定用户的身份。您不能指定其他 windows 用户。这适用于所有 SQL 服务器客户端。如果您想以其他用户身份登录 SQL,则需要 运行 该用户帐户下的 powershell 脚本。
我有一个脚本调用 Invoke-Sqlcmd2
$password = ConvertTo-SecureString 'xxxxxxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('domain\someuser', $password)
$resultListTable = Invoke-Sqlcmd2 -Credential $cred -query $query -ServerInstance "SERVER" -Database ZZZZ -As "DataTable"
失败并显示错误消息
Exception calling "Open" with "0" argument(s): "Login failed for user 'domain\someuser'."
我的 DBA 向我报告说他可以从服务器看到如下所示的错误:
Login failed for user 'domain\someuser' Reason: Attempting to use an NT account name with SQL Server Authentication.
Invoke-SqlCmd2 在 ( https://github.com/sqlcollaborative/Invoke-SqlCmd2/blob/master/Invoke-SqlCmd2/Public/Invoke-SqlCmd2.ps1 ) 的文档表明支持 windows 和 sql 身份验证。 (我试过只使用 sql 凭据,效果很好,但出于商业原因,我需要使用我拥有的 windows 凭据)。而且我已经看到(在某个地方,现在找不到)只需在用户名中提供反斜杠就足以(大概是 System.Data.SqlClient.SQLConnection
对象)进行 select windows 身份验证.不幸的是,这不是我看到的行为。
调用 Windows 身份验证启动时,我还需要做些什么吗?
代码实际做的是:
if ($Credential) {
$CSBuilder["Trusted_Connection"] = $false
$CSBuilder["User ID"] = $Credential.UserName
$CSBuilder["Password"] = $Credential.GetNetworkCredential().Password
}
else {
$CSBuilder["Integrated Security"] = $true
}
(见source code)
...所以基本上,如果您提供凭据,那么它会被假定为 SQL 服务器登录,而不是 windows 登录(因为可信连接设置为 false)。
N.B。您天生就误解了 SQL 服务器的身份验证是如何工作的。 Windows 对 SQL 服务器的授权是基于 当前 登录用户隐式完成的。如果 integrated/trusted 身份验证在连接字符串中设置为打开,则 SQL 将自动使用 windows 用户建立连接的上下文来确定用户的身份。您不能指定其他 windows 用户。这适用于所有 SQL 服务器客户端。如果您想以其他用户身份登录 SQL,则需要 运行 该用户帐户下的 powershell 脚本。