Search code examples
powershellazure-active-directoryazure-powershellazure-analysis-services

How to connect to Azure Analysis Service Server using AAD App Registration in PowerShell


I am able to connect to Azure Analysis Service using my credentials in PowerShell. Can someone tell me how to connect using App Registration. I already added the App Registration as Analysis Services Admins in PowerShell (app:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).

enter image description here

#Using My Credentials
$ServerFullName = "asazure://azureregion.asazure.windows.net/mytestanalysisservice"
$Server = New-Object Microsoft.AnalysisServices.Server
$Server.Connect($ServerFullName)
$Server.Roles.ExternalMembers
$Database = $Server.Databases.Item("adventureworks")

# Using Azure App Registration
$SubscriptionID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$TenantID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$ApplicationID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$PlainPassword = "ClientSecretValue"
$SecuredPassword = ConvertTo-SecureString $PlainPassword -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ApplicationID, $SecuredPassword
Connect-AzAccount -ServicePrincipal -SubscriptionId $SubscriptionID -TenantId $TenantID -Credential $Credential -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
$azContext = Get-AzContext

#Generate Token
$azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
$token = $profileClient.AcquireAccessToken($azContext.Subscription.TenantId)

#Connect to Azure Analysis Service using Token (I need help here)
$ServerFullName = "asazure://azureregion.asazure.windows.net/mytestanalysisservice"
$Server.Connect($ServerFullName)


Solution

  • I was able to connect to Azure Analysis server using App registration Client ID and passwords\ with below ConnectionString. Trick is, not to generate AzToken bearer. Just pass the plainpassword in connectionstring and SSAS (New-Object Microsoft.AnalysisServices.Server) will generate a token and authenticate.

    $ConnectionString = "Provider=MSOLAP;Data Source=asazure://westus.asazure.windows.net/myanalysisinstancename;Initial Catalog=adventureworks;User ID=$UserName;Password=$PlainPassword;Persist Security Info=True;Impersonation Level=Impersonate"

    $Server = New-Object Microsoft.AnalysisServices.Server

    $Server.Connect($ConnectionString)