Friday, 20 May 2016

SSAS XMLA Cube Script Backup

Most of time its taking long time to generate XMLA script for cube.But consider a scenario you have 10 cubes and you want to back of all the cube.XMLA script that what should be the solution. As a developer its hard to wait for generate script one after another in ssms. Below is my finding which will generate all the cube script with specify folder  and it will not make any load on your work not even SSMS will have any impact while you are doing your development.

Run this script in poweshell window and press enter. all cube script will generate in quick.

Step 1: Open powershell

Step 2: Use below code with server and folder location

$serverName = "Servername "
$outputFolder = "Folder Path"

## load the AMO and XML assemblies into the current runspace
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices") > $null
[System.Reflection.Assembly]::LoadWithPartialName("System.Xml") > $null
$dateStamp = (get-Date).ToString("yyyyMMdd")

## connect to the server
$svr = new-Object Microsoft.AnalysisServices.Server
$svr.Connect($serverName)
foreach ($db in $svr.Databases)
{
    write-Host "Scripting: " $db.Name
    $xw = new-object System.Xml.XmlTextWriter("$($outputFolder)DBScript_$($db.Name)_$($dateStamp).xmla", [System.Text.Encoding]::UTF8)
    $xw.Formatting = [System.Xml.Formatting]::Indented
    [Microsoft.AnalysisServices.Scripter]::WriteCreate($xw,$svr,$db,$true,$true)
    $xw.Close()
}
$svr.Disconnect()

Thanks,
Enjoy!!