Skip to content

Instantly share code, notes, and snippets.

@dgosbell
Created January 15, 2019 22:09
Show Gist options
  • Save dgosbell/b1d7991856de6f871497b4217bde96d3 to your computer and use it in GitHub Desktop.
Save dgosbell/b1d7991856de6f871497b4217bde96d3 to your computer and use it in GitHub Desktop.
A simple example of how to manually deploy a model.bim file using Powershell
#Requires -Version 3.0
param(
[Parameter(Mandatory=$true)]
[string]$serverName,
[Parameter(Mandatory=$true)]
[string]$databaseName,
[Parameter(Mandatory=$true)]
[string]$pathToBimFile
)
<#
.SYNOPSIS
Deploys a model.bim file to a server
.DESCRIPTION
Adds a file name extension to a supplied name.
Takes any strings for the file name or extension.
.PARAMETER serverName
The name of the SSAS server which is the deployment target
.PARAMETER databaseName
The name to give to the database we are deploying
.PARAMETER pathToBimFile
The full path the model.bim file
.INPUTS
None. You cannot pipe objects to Add-Extension.
.OUTPUTS
None
.EXAMPLE
The following example will deploy "c:\temp\model.bim" to the localhost\tab17 server
as a database called "Test"
C:\PS>.\deploy-bimfile.ps1 "localhost\tab17" "Test" "c:\temp\model.bim"
#>
## check if the path to the .bim file exists
if (-not (test-path $pathToBimFile)) {
write-error "Could not find the bim file: $pathToBimFile"
exit 1
}
## Load AMO Library
[Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices") > $null
## Connect to Server
$server = New-Object Microsoft.AnalysisServices.Server
$server.connect($serverName)
$json = Get-Content $pathToBimFile -Raw
$db = [Microsoft.AnalysisServices.JsonSerializer]::DeserializeDatabase($json)
$db.Name = $databaseName
$db.ID = $databaseName
####
## at this point we have a Database object and we can alter
## any properties such as data source connection strings
## before generating the final createOrReplace script
####
$script = [Microsoft.AnalysisServices.Tabular.JsonScripter]::ScriptCreateOrReplace($db)
$server.Execute($script)
$server.Disconnect()
@Ashish-Jovial
Copy link

Dear @dgosbell,

Really great work. As I can see line no. 58 will open a popup to connect with the server. I am curious how this script will be used for automation (using Automation Account's runbooks or Azure PowerShell Task within CI/CD pipelines)? Do you know any mechanism to suppress the popup and perform the login using PowerShell?

@dgosbell
Copy link
Author

dgosbell commented Jul 4, 2021

This script was originally written for an on-prem environment so there was no Azure AD pop up to deal with. But I think you could probably use the Login-AzureAsAccount to connect as a service principal see the following for an example script which uses that cmdlet https://azure.microsoft.com/en-us/blog/automation-of-azure-analysis-services-with-service-principals-and-powershell/

@Kazanskyi
Copy link

Hello @dgosbell,
Thank you for your script!
I am trying to run it for PowerBI SSAS model and I gen an error:
Unable to find type [Microsoft.AnalysisServices.JsonSerializer].

Could you help to resolve it? I can't find a simple answer online.

@dgosbell
Copy link
Author

@Kazanskyi - that probably means you have an older version of the client libraries installed on your machine. The simplest fix would be to install the most recent version by down loading the AMO installer from here https://docs.microsoft.com/en-us/analysis-services/client-libraries?view=asallproducts-allversions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment