In this post I’ll show you how to Use PowerShell to Enable and Automate Azure Analysis Backup.

Enable Azure Analysis Service Backup

Enable-AzureRmAnalysisServicesBackup is a small powershell script that uses the the Set-AzureRmResource cmdlet to enable backup location to an Azure Analysis Service instance.

Once you run it, the script will ask you for:

  • The resource group name where the Analysis Service is deployed
  • The name of the Azure Analysis Service instance.
  • The name of te storage account where the backups will be placed
  • The name of the container in the storage accoutn where the backups will be placed
 1Param
 2(
 3    [Parameter(Mandatory = $true)][string]$resourceGroupName,
 4    [Parameter(Mandatory = $true)][string]$analysisServicesName,
 5    [Parameter(Mandatory = $true)][string]$storageAccountName,
 6    [Parameter(Mandatory = $true)][string]$containerName)
 7
 8function Add-AnalysisServicesBackupBlobContainer($props, $resourceGroupName, $storageAccountName, $containerName) {
 9    # Get storage account keys.
10    $keys = Get-AzureRmStorageAccountKey `
11        -StorageAccountName $storageAccountName `
12        -ResourceGroupName $resourceGroupName
13
14    # Use first key.
15    $key = $keys[0].Value
16
17    # Create an Azure Storage Context for the storage account.
18    $context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $key
19
20    # Create a 2 Years SAS Token
21    $starTime = (Get-Date).AddMinutes(-5)
22    $sasToken = New-AzureStorageContainerSASToken `
23        -Name $containerName `
24        -Context $context `
25        -ExpiryTime $starTime.AddYears(2) `
26        -Permission rwdlac `
27        -Protocol HttpsOnly `
28        -StartTime $starTime
29
30    # Create the Container Uri
31    $blobContainerUri = "https://$($storageAccountName).blob.core.windows.net/$($containerName)$($sasToken)"
32
33    # Check the Analysis Server to see if the backupBlobContainerUri property exists.
34    $backupBlobContainerUriProperty = $props | Get-Member -Name "backupBlobContainerUri"
35    if (!$backupBlobContainerUriProperty) {
36        # Add the property to the object.
37        $props | Add-Member @{backupBlobContainerUri = ""}
38    }
39
40    # Set the container Uri
41    $props.backupBlobContainerUri = $blobContainerUri
42}
43
44# Get the Analysis Services resource properties.
45$resource = Get-AzureRmResource `
46    -ResourceGroupName $resourceGroupName `
47    -ResourceType "Microsoft.AnalysisServices/servers" `
48    -ResourceName $analysisServicesName
49
50$props = $resource.Properties
51
52# Modify the backupBlobContainerUri.
53Add-AnalysisServicesBackupBlobContainer $props $resourceGroupName $storageAccountName $containerName
54
55# Save the properties.
56Set-AzureRmResource `
57    -PropertyObject $props `
58    -ResourceGroupName $resourceGroupName `
59    -ResourceType "Microsoft.AnalysisServices/servers" `
60    -ResourceName $analysisServicesName `
61    -Force

Get the code here or download the file from the Technet Gallery.

Automate Azure Analysis Service Backup

Backup-AnalysisServices is a simple PowerShell workflow runbook that will help you automate the process of backing up an Azure Analysis Service Database.

The script receives 5 parameters:

  • ResourceGroupName: The name of the resource group where the cluster resides
  • AutomationCredentialName: The Automation credential holding username and password for Analysis Services
  • AnalysisServiceDatabase: The Analysis Service Database
  • AnalysisServiceServer: The Analysis Service Server (i.e. asazure://northeurope.asazure.windows.net/myanalysisservice)
  • ConnectionName: The name of your automation connection account. Defaults to ‘AzureRunAsConnection'
 1<#
 2    .SYNOPSIS
 3        Backup-AnalysisServices is a simple PowerShell workflow runbook that will help you automate the process of backing up an Azure Analysis Service Database.
 4
 5    .DESCRIPTION
 6        Backup-AnalysisServices is a simple PowerShell workflow runbook that will help you automate the process of backing up an Azure Analysis Service Database.
 7
 8    .PARAMETER ResourceGroupName
 9        The name of the resource group where the cluster resides
10
11    .PARAMETER AutomationCredentialName
12        The Automation credential holding the username and password for Analysis Services
13
14    .PARAMETER AnalysisServiceDatabase
15        The Analysis Service Database
16
17    .PARAMETER AnalysisServiceServer
18        The Analysis Service Server
19
20    .PARAMETER ConnectionName
21        The name of your automation connection account. Defaults to  'AzureRunAsConnection'
22
23    .NOTES
24        AUTHOR: Carlos Mendible
25        LASTEDIT: October 17, 2017
26#>
27workflow Backup-AnalysisServices {
28    Param
29    (
30        [Parameter(Mandatory = $true)]
31        [String]$ResourceGroupName,
32
33        [Parameter(Mandatory = $true)]
34        [String]$AutomationCredentialName,
35
36        [Parameter(Mandatory = $true)]
37        [String]$AnalysisServiceDatabase,
38
39        [Parameter(Mandatory = $true)]
40        [String]$AnalysisServiceServer,
41        [Parameter(Mandatory = $false)]
42        [String]$ConnectionName
43    )
44
45    # Requires the AzureRM.Profile and SqlServer PowerShell Modules
46
47    $automationConnectionName = $ConnectionName
48    if (!$ConnectionName) {
49        $automationConnectionName = "AzureRunAsConnection"
50    }
51
52    # Get the connection by name (i.e. AzureRunAsConnection)
53    $servicePrincipalConnection = Get-AutomationConnection -Name $automationConnectionName
54
55    Write-Output "Logging in to Azure..."
56
57    Add-AzureRmAccount `
58        -ServicePrincipal `
59        -TenantId $servicePrincipalConnection.TenantId `
60        -ApplicationId $servicePrincipalConnection.ApplicationId `
61        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
62
63    # Get PSCredential
64    $cred = Get-AutomationPSCredential -Name $AutomationCredentialName
65
66    Write-Output "Starting Backup..."
67
68    Backup-ASDatabase `
69        backupfile ("backup." + (Get-Date).ToString("yyMMdd") + ".abf") `
70        name $AnalysisServiceDatabase `
71        -server $AnalysisServiceServer `
72        -Credential $cred
73}

Get the code here or download the file from the Technet Gallery.

Hope it helps!