Active Directory Password Connection Azure SQL Failure from Azure Automation

前端 未结 3 1221
慢半拍i
慢半拍i 2020-12-21 06:33

I need to connect via Azure Automation to an Azure SQL Server using my Azure Active Directory Admin account that is set as the Azure SQL Server AZ AD Admin.

I am ab

3条回答
  •  清酒与你
    2020-12-21 07:28

    Using the Azure Automation Module

       ## 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()
    

    Code for inside 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
    

提交回复
热议问题