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

时间:2017-07-13 21:24:12

标签: sql-server powershell azure azure-automation

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

我能够连接到Azure SQL:

  1. 将SSMS与Azure AD管理员帐户一起使用
  2. 在SQL ConnectionString中使用PowerShell ISE和Azure AD管理员帐户
  3. 在SQL ConnectionString中使用Azure自动化与Azure SQL管理员帐户(在创建新的Azure SQL Server时创建的帐户)
  4. 但是,在SQL ConnectionString中使用Azure自动化中的Active Directory管理员帐户尝试连接Azure Azure中的Azure SQL时,出现以下错误:

      

    New-Object:异常调用" .ctor"用" 1"参数:"关键字   不支持:'身份验证'。"

    这是我的连接尝试:

    $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自动化与Azure Active Directory管理员连接的任何想法?我还可以通过Azure自动化和Azure SQL管理员帐户(您使用Azure SQL创建的默认管理员帐户)进行连接。

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

3 个答案:

答案 0 :(得分:3)

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

建议:

答案 1 :(得分:1)

使用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

答案 2 :(得分:1)

请参阅此类question

如果要将SQL Server与Azure AD用户连接,则应在VM上安装ADAL SQL library。现在,Azure自动化帐户不安装库。如果要使用Azure AD用户登录SQL Server,可以选择hybrid workers

  

Azure自动化中的Runbook无法访问本地资源   数据中心,因为它们在Azure云中运行。混合Runbook   Azure自动化的工作程序功能允许您运行Runbook   位于数据中心的计算机用于管理本地资源。该   Runbooks在Azure自动化中存储和管理,然后交付   到一台或多台本地机器。

相关问题