Last active
January 21, 2025 23:44
-
-
Save dgosbell/8eefc9e25f9063eeb23faf66a4e3ce41 to your computer and use it in GitHub Desktop.
Uses the XMLA endpoint to add a user to a role for a Power BI dataset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## NOTE: editing your dataset via the XMLA endpoint will prevent you from downloading it | |
## so make sure you have a backup of the original pbix file | |
$clientID = '<your-clientId>' | |
$clientSecret = '<your-client-secret>' | |
$tenantID = '<your-teantId>' | |
$workspaceName = 'Xmla%20Test' | |
$datasetName = 'Role Test' | |
$roleName = 'DynamicRole' | |
$userToAdd = '[email protected]' | |
# Load the TOM / AMO assembly | |
import-module sqlserver | |
## Connect to Power BI XMLA Endpoint | |
[Microsoft.AnalysisServices.Server]$svr = new-Object([Microsoft.AnalysisServices.Server]) | |
$svr.Connect("data source=powerbi://api.powerbi.com/v1.0/myorg/$workspaceName;User ID=app:$clientID@$tenantID;Password=$clientSecret") | |
## Get a reference to the dataset | |
$dataset = $svr.Databases.GetByName($datasetName) | |
$model = $dataset.Model | |
## get the role from the model | |
$role = $model.roles[$roleName] | |
## add a member to a role | |
$member = new-object Microsoft.AnalysisServices.Tabular.ExternalModelRoleMember | |
$member.MemberType = [Microsoft.AnalysisServices.Tabular.RoleMemberType]::User | |
$member.MemberName = $userToAdd | |
$member.IdentityProvider = "AzureAD" | |
$role.Members.Add($member) | |
$model.SaveChanges() | |
$svr.Disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment