Microsoft Azure Documentation

Deploy Azure Analysis Services Models using PowerShell

This guide provides step-by-step instructions and PowerShell scripts to automate the deployment of your Azure Analysis Services models. Automating deployments is crucial for CI/CD pipelines, enabling consistent and repeatable deployments.

This article focuses on deploying existing model files (e.g., TOM model definition files) to an Azure Analysis Services instance. It does not cover the creation or modification of the model itself.

Prerequisites

Steps to Deploy a Model

1. Export Your Model Definition

Before you can deploy, you need a deployable representation of your model. The most common way is to use the TOM (Tabular Object Model) XMLA file generated from your model project.

Assume you have a file named MyCubeModel.asdatabase which contains the model definition in TOM XMLA format.

2. Create the Deployment PowerShell Script

The following script uses the Invoke-ASCmd cmdlet to execute XMLA commands against your Analysis Services instance. This script connects to your server, drops the existing database (optional but common for full redeploys), and then deploys the new model definition.

Save the following code as a .ps1 file (e.g., Deploy-AASModel.ps1):



param(
    [Parameter(Mandatory=$true)]
    [string]$ServerName,

    [Parameter(Mandatory=$true)]
    [string]$DatabaseName,

    [Parameter(Mandatory=$true)]
    [string]$ModelDefinitionFile,

    [Parameter(Mandatory=$false)]
    [switch]$DropDatabaseIfExist
)

# --- Connection and Authentication ---
# For Azure AS, you typically need to authenticate using Azure AD.
# The SqlServer module handles this automatically if you're logged in via Azure CLI or PowerShell.
# Ensure you are logged in: Connect-AzAccount

Write-Host "Connecting to Azure Analysis Services server: $ServerName..."

# --- Check if database exists ---
$dbExists = $false
try {
    $databases = Invoke-ASCmd -Server $ServerName -Query "SHOW GRANULARITY OF DATABASES"
    if ($databases -match "\[$DatabaseName\]") {
        $dbExists = $true
        Write-Host "Database '$DatabaseName' already exists."
    }
} catch {
    Write-Warning "Could not retrieve database list. It might not exist or permissions are insufficient. Error: $($_.Exception.Message)"
}

# --- Drop database if specified and exists ---
if ($DropDatabaseIfExist -and $dbExists) {
    Write-Host "Dropping existing database '$DatabaseName' on '$ServerName'..."
    $dropXmla = @"

  
    
      <Database Name="$DatabaseName" ID="$DatabaseName">
        <DropDatabase>true</DropDatabase>
      </Database>
    
  

"@
    try {
        Invoke-ASCmd -Server $ServerName -InputObject $dropXmla
        Write-Host "Database '$DatabaseName' dropped successfully."
        $dbExists = $false # Database is now gone
    } catch {
        Write-Error "Failed to drop database '$DatabaseName'. Error: $($_.Exception.Message)"
        exit 1
    }
} elseif ($DropDatabaseIfExist -and !$dbExists) {
    Write-Host "Database '$DatabaseName' does not exist, no need to drop."
}

# --- Deploy the new model definition ---
if (-not (Test-Path $ModelDefinitionFile)) {
    Write-Error "Model definition file not found: $ModelDefinitionFile"
    exit 1
}

$modelXmla = Get-Content $ModelDefinitionFile -Raw

Write-Host "Deploying model from '$ModelDefinitionFile' to database '$DatabaseName' on '$ServerName'..."

try {
    # Use Alter command with AllowCreate=true to create or update the database
    $deployXmla = @"

  
    
      $modelXmla
    
  

"@
    Invoke-ASCmd -Server $ServerName -InputObject $deployXmla
    Write-Host "Model deployment to '$DatabaseName' on '$ServerName' completed successfully."

} catch {
    Write-Error "Model deployment failed. Error: $($_.Exception.Message)"
    exit 1
}

Write-Host "Deployment process finished."

3. Execute the PowerShell Script

Before running, ensure you have authenticated to Azure:

Connect-AzAccount

Then, execute the script from your PowerShell console, providing the necessary parameters:


.\Deploy-AASModel.ps1 `
    -ServerName "your-aas-server-name.asazure.windows.net" `
    -DatabaseName "YourDatabaseName" `
    -ModelDefinitionFile "C:\path\to\your\MyCubeModel.asdatabase" `
    -DropDatabaseIfExist

Explanation of Parameters:

  • -ServerName: The fully qualified domain name of your Azure Analysis Services server.
  • -DatabaseName: The name you want for the Analysis Services database on your server.
  • -ModelDefinitionFile: The full path to your TOM XMLA file.
  • -DropDatabaseIfExist: An optional switch. If present, the script will drop the existing database with the specified name before deploying the new one. Use with caution.

Advanced Considerations

  • Incremental Deployment: For larger models or frequent updates, consider using TOM cmdlets directly or tools that support incremental deployments to avoid redeploying the entire model.
  • Connection Strings: The script assumes you are logged into Azure. For service principals or other authentication methods, you'll need to adapt the connection logic.
  • Error Handling: Enhance error handling and logging for production scenarios.
  • Parameters: You can modify the TOM XMLA file or script to handle dynamic parameters (e.g., dates, configurations) during deployment.
  • CI/CD Integration: Integrate this script into Azure DevOps, GitHub Actions, or other CI/CD tools for fully automated deployments.

By leveraging PowerShell and the Analysis Services module, you can streamline your Azure Analysis Services deployment process, ensuring faster and more reliable updates to your data models.