每次我使用集成安全时,TRUE 或 SSPI 都使用登录的计算机帐户

Every time I use integrated security, either TRUE or SSPI is uses the logged in computer account

下面的函数很好用....但不是真的。如果我 运行 powershell 作为另一个用户使用它...我 运行 azure automation 并且当它通过工作时我得到以下信息:

Login failed for user 'corp\server01$'. Reason: Could not find a login matching the name provided. [CLIENT: 192.168.1.1]

我使用以下函数:

Function SQL-Query {
    param(
        $server,
        $database,
        $username,
        $password,
        $sqlquery
    )
    $SqlConnection = New-Object System.Data.SqlClient.SqlConnection
    $SqlConnection.ConnectionString = "Server = $server; Database = $database; TrustServerCertificate = True; User ID = $Username; Password = $Password; integrated security = sspi;"
    $SqlCmd = New-Object System.Data.SqlClient.SqlCommand
    $SqlCmd.CommandText = $sqlquery
    $SqlCmd.Connection = $SqlConnection
    $SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
    $SqlAdapter.SelectCommand = $SqlCmd
    $DataSet = New-Object System.Data.DataSet
    $SqlAdapter.Fill($DataSet)
    $SqlConnection.Close()
    return $DataSet.Tables[0]
}

好吧,你会说设置 'integrated security = false' 是的,然后我明白了:

Login failed for user ''. Reason: An attempt to login using SQL authentication failed. Server is configured for Windows authentication only. [CLIENT: 192.168.1.1]

好的然后你会说确保启用混合模式...我明白了

exec master.sys.xp_loginconfig 'login mode'
login mode  Mixed

那么你可能会说使用身份验证=活动目录密码:

Login failed for user ''. Reason: An attempt to login using SQL authentication failed. Server is configured for Windows authentication only. [CLIENT: 192.168.1.1]

没有任何效果。我想要做的就是使用 powershell 针对此数据库使用活动目录用户名和密码。

是的,我知道我可以使用将混合工作者设置为 运行 作为另一个帐户......但是假设我没有使用 azure 自动化,我怎么能在没有登录的情况下传递 AD 信用那个帐户。在我看来,这是一件很常见的事情。

您目前 运行 在服务器的本地系统帐户(计算机帐户)下,如 corp\server01$ 末尾的 $ 符号所示。这未被授权连接到您的 SQL 服务器实例。

当连接字符串包含 Integrated security=true(Windows 身份验证)时,usernamepassword 字段将被忽略,因为它们仅与 SQL 相关验证。 Windows 身份验证使用缓存的 Windows 客户端应用程序凭据,在本例中为 Azure 自动化作业。

您需要设置一个 RunAs account 以便 Azure 自动化作业有一个帐户可以使用。
如果需要,您可以将计算机帐户添加到 MSSQL 中的 Windows 身份验证用户组,但这听起来不太理想。