Active Directory 密码连接 Azure SQL Azure 自动化失败

Active Directory Password Connection Azure SQL Failure from Azure Automation

我需要使用设置为 Azure SQL 服务器 AZ AD 管理员的 Azure Active Directory 管理员帐户通过 Azure 自动化连接到 Azure SQL 服务器。

我可以连接到 Azure SQL:

  1. 通过 Azure AD 管理员帐户使用 SSMS
  2. 在 SQL ConnectionString
  3. 中将 PowerShell ISE 与 Azure AD 管理员帐户结合使用
  4. 在 SQL ConnectionString
  5. 中将 Azure 自动化与 Azure SQL 管理员帐户(在创建新的 Azure SQL 服务器时创建的帐户)一起使用

但是,当尝试在 SQL ConnectionString 中使用 Azure 自动化中的 Active Directory 管理员帐户连接到 Azure 自动化中的 Azure SQL 时,出现以下错误:

New-Object : Exception calling ".ctor" with "1" argument(s): "Keyword not supported: 'authentication'."

这是我的连接尝试:

$server = "tcp:myazuresql.database.windows.net,1433"
$database = "TestDB"
$adminName = "test@mytest.onmicrosoft.com"
$adminPassword = "test1234"

$connectionString = "Server=$server;Database=$database;User ID=$adminName;Password=$adminPassword;authentication=Active Directory Password;"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)

关于为什么我可以通过 PowerShell ISE 和 SSMS 连接而不是 Azure Automation 与 Azure Active Directory Admin 连接有什么想法吗?我还可以通过 Azure 自动化和 Azure SQL 管理员帐户(您使用 Azure SQL 创建的默认管理员帐户)进行连接。

我无法连接的唯一方法是在使用 Azure 自动化时使用绑定到 Azure SQL 的 Azure Active Directory Admin。

使用 Azure 自动化模块

   ## Using Azure Automation ISE Add-on
    #Install-Module -Name AzureAutomationAuthoringToolkit
    Import-Module AzureAutomationAuthoringToolkit
    $SqlServer = "myazuresql.database.windows.net"
    $SqlServerPort = "1433"
    $Database = "TestDB"
    $Table = ""
    $SqlCredentialAsset = ""
    $SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset 
    if ($SqlCredential -eq $null) 
        { 
            throw "Could not retrieve '$SqlCredentialAsset' credential asset. Check that you created this first in the Automation service." 
        }   
    $SqlUsername = $SqlCredential.UserName 
    $SqlPass = $SqlCredential.GetNetworkCredential().Password 
    $Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer,$SqlServerPort;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")

    $Conn.Open() 
    $Cmd=new-object system.Data.SqlClient.SqlCommand("SELECT COUNT(*) from $Table", $Conn) 
    $Cmd.CommandTimeout=120 
    $Conn.Close()

RunBook 内部代码

#Runbook
Param
(
[Parameter(Mandatory=$true)]
[String]
$AureConnectionName
)

$AzureConn = Get-AutomationConnection -Name $AzureConnectionName

If ($AuzreConn -eq $null)
{
    throw "Could not retrieve '$SqlCredentialAsset' credential asset."
}
$Certificate = Get-AutomationCertificate -Name $AzureConn.AutomationCertificateName

if ($Certificate -eq $null)
{
 throw "Could not retrieve '$AzureConn.AutomationCertificateName' certificate asset." 
}

$cred = Get-Credential -Credential Domain\User
Login-AzureRmAccount -Credential $cred
Get-AzureRmSubscription | Select-AzureRmSubscription

请参考这篇类似的

如果您想将 SQL 服务器与 Azure AD 用户连接,ADAL SQL library should install on your VM. Now, Azure automation account does not install library. If you want use Azure AD user login your SQL server, you could select hybrid workers

Runbooks in Azure Automation cannot access resources in your local data center since they run in the Azure cloud. The Hybrid Runbook Worker feature of Azure Automation allows you to run runbooks on machines located in your data center to manage local resources. The runbooks are stored and managed in Azure Automation and then delivered to one or more on-premises machines.

Azure 自动化尚不支持使用 Azure AD 连接 SQL Account.This 功能需要 .NET Framework 4.6,目前 Azure 自动化工作者只有 .NET Framework 4.5。

建议: