Skip to content

Instantly share code, notes, and snippets.

@dgosbell
Last active June 25, 2023 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgosbell/8eefc9e25f9063eeb23faf66a4e3ce41 to your computer and use it in GitHub Desktop.
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
## 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 = 'test@gosbell.com'
# 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