Skip to content

Instantly share code, notes, and snippets.

@albertospelta
Last active May 6, 2023 19:06
Show Gist options
  • Save albertospelta/37a6b69772fb31ea1571ed73683cc421 to your computer and use it in GitHub Desktop.
Save albertospelta/37a6b69772fb31ea1571ed73683cc421 to your computer and use it in GitHub Desktop.
An example of how to query a PowerBI dataset using Powershell and AdomdClient
$ErrorActionPreference = "Stop"
function LoadAdomdClientPackage() {
$package = Get-Package -Name Microsoft.AnalysisServices.AdomdClient.retail.amd64 -ErrorAction SilentlyContinue
if (!$package) {
Install-Package -Name Microsoft.AnalysisServices.AdomdClient.retail.amd64 -RequiredVersion 19.61.1.4 -ProviderName NuGet -Source "https://www.nuget.org/api/v2" -Scope CurrentUser -SkipDependencies -Force
$package = Get-Package -Name Microsoft.AnalysisServices.AdomdClient.retail.amd64
}
$assemblyPath = Join-Path (Get-ChildItem $package.Source).DirectoryName "lib\net45\Microsoft.AnalysisServices.AdomdClient.dll"
try { Add-Type -Path $assemblyPath} catch { $_.Exception.LoaderExceptions }
}
function ExecuteAdomdQuery($connectionString, $query) {
$connection = New-Object Microsoft.AnalysisServices.AdomdClient.AdomdConnection;
try {
$connection.ConnectionString = $connectionString
$connection.Open();
$table = New-Object System.Data.DataTable
$adapter = New-Object Microsoft.AnalysisServices.AdomdClient.AdomdDataAdapter $query, $connection
$adapter.Fill($table)
$connection.Close();
return $table
}
finally {
$connection.Dispose();
}
}
#$bearerToken=''
#$databaseId='00000000-0000-0000-0000-000000000000'
#$connectionString = "Provider=MSOLAP;Data Source=pbiazure://api.powerbi.com;Initial Catalog=sobe_wowvirtualserver-$databaseId;Integrated Security=ClaimsToken;Password=$bearerToken"
$workspaceName='' # use an empty string to connect to "My Workspace"
$databaseName='Contoso-myworkspace'
$connectionString = "Provider=MSOLAP;Data Source=powerbi://api.powerbi.com/v1.0/myorg/$workspaceName;Initial Catalog=$databaseName;"
LoadAdomdClientPackage;
$table = ExecuteAdomdQuery $connectionString "EVALUATE { 1 }";
$table | Format-Table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment