Refresh with Azure Automation

By using Azure Automation and PowerShell Runbooks, you can perform automated data refresh operations on your Azure Analysis tabular models.

The example in this article uses the PowerShell SqlServer modules.

A sample PowerShell Runbook, which demonstrates refreshing a model is provided later in this article.

Authentication

All calls must be authenticated with a valid Azure Active Directory (OAuth 2) token. The example in this article will use a Service Principal (SPN) to authenticate to Azure Analysis Services.

To learn more about creating a Service Principal, see Create a service principal by using Azure portal.

Prerequisites

Important

The following example assumes the Azure Analysis Services firewall is disabled. If the firewall is enabled, then the public IP address of the request initiator will need to be whitelisted in the firewall.

  1. In your Azure Automation Account, Click Modules, then Browse gallery.

  2. In the search bar, search for SqlServer.

    Search Modules

  3. Select SqlServer, then click Import.

    Import Module

  4. Click OK.

Create a Service Principal (SPN)

To learn about creating a Service Principal, see Create a service principal by using Azure portal.

Configure permissions in Azure Analysis Services

The Service Principal you create must have server administrator permissions on the server. To learn more, see Add a service principal to the server administrator role.

Design the Azure Automation Runbook

  1. In the Automation Account, create a Credentials resource which will be used to securely store the Service Principal.

    Create credential

  2. Enter the details for the credential. For the User name, enter the SPN ClientId, for the Password, enter the SPN Secret.

    Create credential

  3. Import the Automation Runbook

    Import Runbook

  4. Browse for the Refresh-Model.ps1 file, provide a Name and description, and then click Create.

    Import Runbook

  5. When the Runbook has been created, it will automatically go into edit mode. Select Publish.

    Publish Runbook

    Note

    The credential resource that was created previously is retrieved by the runbook by using the Get-AutomationPSCredential command. This command is then passed to the Invoke-ProcessASADatabase PowerShell command to perform the authentication to Azure Analysis Services.

  6. Test the runbook by clicking Start.

    Start the Runbook

  7. Fill out the DATABASENAME, ANALYSISSERVER, and REFRESHTYPE parameters, and then click OK. The WEBHOOKDATA parameter is not required when the Runbook is run manually.

    Start the Runbook

If the Runbook executed successfully, you will receive an output like the following:

Successful Run

Use a self-contained Azure Automation Runbook

The Runbook can be configured to trigger the Azure Analysis Services model refresh on a scheduled basis.

This can be configured as follows:

  1. In the Automation Runbook, click Schedules, then Add a Schedule.

    Create schedule

  2. Click Schedule > Create a new schedule, and then fill in the details.

    Configure schedule

  3. Click Create.

  4. Fill in the parameters for the schedule. These will be used each time the Runbook triggers. The WEBHOOKDATA parameter should be left blank when running via a schedule.

    Configure parameters

  5. Click OK.

Consume with Data Factory

To consume the runbook by using Azure Data Factory, first create a Webhook for the runbook. The Webhook will provide a URL which can be called via an Azure Data Factory web activity.

Important

To create a Webhook, the status of the Runbook must be Published.

  1. In your Automation Runbook, click Webhooks, and then click Add Webhook.

    Add Webhook

  2. Give the Webhook a name and an expiry. The name only identifies the Webhook inside the Automation Runbook, it doesn't form part of the URL.

    Caution

    Ensure you copy the URL before closing the wizard as you cannot get it back once closed.

    Configure Webhook

    The parameters for the webhook can remain blank. When configuring the Azure Data Factory web activity, the parameters can be passed into the body of the web call.

  3. In Data Factory, configure a web activity

Example

Example Web Activity

The URL is the URL created from the Webhook.

The body is a JSON document which should contain the following properties:

Property Value
AnalysisServicesDatabase The name of the Azure Analysis Services database
Example: AdventureWorksDB
AnalysisServicesServer The Azure Analysis Services server name.
Example: https://westus.asazure.windows.net/servers/myserver/models/AdventureWorks/
DatabaseRefreshType The type of refresh to perform.
Example: Full

Example JSON body:

{
    "AnalysisServicesDatabaseName": "AdventureWorksDB",
    "AnalysisServicesServer": "asazure://westeurope.asazure.windows.net/MyAnalysisServer",
    "DatabaseRefreshType": "Full"
}

These parameters are defined in the runbook PowerShell script. When the web activity is executed, the JSON payload passed is WEBHOOKDATA.

This is deserialized and stored as PowerShell parameters, which are then used by the Invoke-ProcesASDatabase PowerShell command.

Deserialized Webhook

Use a Hybrid Worker with Azure Analysis Services

An Azure Virtual Machine with a static public IP address can be used as an Azure Automation Hybrid Worker. This public IP address can then be added to the Azure Analysis Services firewall.

Important

Ensure the Virtual Machine public IP address is configured as static.

To learn more about configuring Azure Automation Hybrid Workers, see Automate resources in your datacenter or cloud by using Hybrid Runbook Worker.

Once a Hybrid Worker is configured, create a Webhook as described in the section Consume with Data Factory. The only difference here is to select the Run on > Hybrid Worker option when configuring the Webhook.

Example webhook using Hybrid Worker:

Example Hybrid Worker Webhook

Sample PowerShell Runbook

The following code snippet is an example of how to perform the Azure Analysis Services model refresh using a PowerShell Runbook.

param
(
    [Parameter (Mandatory = $false)]
    [object] $WebhookData,

    [Parameter (Mandatory = $false)]
    [String] $DatabaseName,
    [Parameter (Mandatory = $false)]
    [String] $AnalysisServer,
    [Parameter (Mandatory = $false)]
    [String] $RefreshType
)

$_Credential = Get-AutomationPSCredential -Name "ServicePrincipal"

# If runbook was called from Webhook, WebhookData will not be null.
if ($WebhookData)
{ 
    # Retrieve AAS details from Webhook request body
    $atmParameters = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
    Write-Output "CredentialName: $($atmParameters.CredentialName)"
    Write-Output "AnalysisServicesDatabaseName: $($atmParameters.AnalysisServicesDatabaseName)"
    Write-Output "AnalysisServicesServer: $($atmParameters.AnalysisServicesServer)"
    Write-Output "DatabaseRefreshType: $($atmParameters.DatabaseRefreshType)"
    
    $_databaseName = $atmParameters.AnalysisServicesDatabaseName
    $_analysisServer = $atmParameters.AnalysisServicesServer
    $_refreshType = $atmParameters.DatabaseRefreshType
 
    Invoke-ProcessASDatabase -DatabaseName $_databaseName -RefreshType $_refreshType -Server $_analysisServer -ServicePrincipal -Credential $_credential
}
else 
{
    Invoke-ProcessASDatabase -DatabaseName $DatabaseName -RefreshType $RefreshType -Server $AnalysisServer -ServicePrincipal -Credential $_Credential
}

Next steps

Samples
REST API