-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathPowerBI_XmlaRefreshSpecifiedTable.ps1
52 lines (47 loc) · 2.36 KB
/
PowerBI_XmlaRefreshSpecifiedTable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# TMSL Script
# This script helps you refresh a specified table in a specified dataset
# Please know this leverages Power BI XMLA endpoints and therefor Power BI Premium or Premium per user.
# For full documenation, please look at: https://docs.microsoft.com/en-us/analysis-services/tmsl/refresh-command-tmsl?WT.mc_id=DP-MVP-5003435&view=asallproducts-allversions
# Also check my blogpost on this topic here: https://data-marc.com/2021/02/18/trigger-a-single-table-to-refresh-in-the-power-bi-service/
# Run parameters, please specify below parameters
$WorkspaceName = "{Your workspace name here}" #Here it is the workspace name! Not the id!
$DatasetName = "{Your dataset name here}" #Also known as database name
$TableName = "{Your Table name here}" #Table name in the specified dataset
# Base variables
$PbiBaseConnection = "powerbi://api.powerbi.com/v1.0/myorg/"
$XmlaEndpoint = $PbiBaseConnection + $WorkspaceName
# Check whether the SQL Server module is installed. If not, it will be installed.
# Install Module (Admin permissions might be required)
$moduleName = Get-Module -ListAvailable -Verbose:$false | Where-Object { $_.Name -eq "SqlServer" } | Select-Object -ExpandProperty Name;
if ([string]::IsNullOrEmpty($moduleName)) {
Write-Host -ForegroundColor White "==============================================================================";
Write-Host -ForegroundColor White "Install module SqlServer...";
Install-Module SqlServer -RequiredVersion 21.1.18230 -Scope CurrentUser -SkipPublisherCheck -AllowClobber -Force
# Check for the latest version this documentation: https://www.powershellgallery.com/packages/SqlServer/
Write-Host -ForegroundColor White "==============================================================================";
}
# TMSL Script
$TmslScript =
@"
{
"refresh": {
"type": "automatic",
"objects": [
{
"database": "$DatasetName",
"table": "$TableName"
}
]
}
}
"@
# Execute refresh trigger on specified table
Try {
Invoke-ASCmd -Query $TmslScript -Server: $XmlaEndpoint -Database $DatasetName
# Write message if succeeded
Write-Host "Table" $TableName "in dataset" $DatasetName "successfully triggered to refresh" -ForegroundColor Green
}
Catch{
# Write message if error
Write-Host "An error occured" -ForegroundColor Red
}