-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathAdmin-AddUserToWorkspace.ps1
55 lines (40 loc) · 1.69 KB
/
Admin-AddUserToWorkspace.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
53
#Requires -Modules @{ ModuleName="MicrosoftPowerBIMgmt.Profile"; ModuleVersion="1.2.1026" }
#Requires -Modules @{ ModuleName="MicrosoftPowerBIMgmt.Workspaces"; ModuleVersion="1.2.1026" }
param(
# See more examples here: https://learn.microsoft.com/en-us/rest/api/power-bi/admin/groups-add-user-as-admin
$identity = "[email protected]",
$identityType = "User",
$workspaceRole = "Member",
$workspaces = @("664e5e57-47a2-4cbc-9539-99da11abf341"),
$servicePrincipalId = "",
$servicePrincipalSecret = "",
$servicePrincipalTenantId = ""
)
$ErrorActionPreference = "Stop"
$VerbosePreference = "SilentlyContinue"
$currentPath = (Split-Path $MyInvocation.MyCommand.Definition -Parent)
Set-Location $currentPath
# Get token with admin account
if ($servicePrincipalId)
{
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $servicePrincipalId, ($servicePrincipalSecret | ConvertTo-SecureString -AsPlainText -Force)
$pbiAccount = Connect-PowerBIServiceAccount -ServicePrincipal -Tenant $servicePrincipalTenantId -Credential $credential
}
else {
$pbiAccount = Connect-PowerBIServiceAccount
}
Write-Host "Login with: $($pbiAccount.UserName)"
Write-Host "Workspaces to set security: $($workspaces.Count)"
foreach($workspace in $workspaces)
{
Write-Host "Adding identity to workspace: $workspace)"
$body = @{
"identifier" = $identity
;
"groupUserAccessRight" = $workspaceRole
;
"principalType" = $identityType
}
$bodyStr = ($body | ConvertTo-Json)
Invoke-PowerBIRestMethod -method Post -url "admin/groups/$workspace/users" -body $bodyStr
}