-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChange-Workspace_Ownership-PowerBI.ps1
51 lines (41 loc) · 1.69 KB
/
Change-Workspace_Ownership-PowerBI.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
# Install AzureAD module if not already installed
if (-not (Get-Module -Name AzureAD -ListAvailable)) {
Install-Module -Name AzureAD -Force -Scope CurrentUser
}
# Import the AzureAD module
Import-Module AzureAD -Force
# Specify your app ID and client secret
$clientId = "MyClientID"
$tenantId = "MyTenant"
$clientSecret = "MySecret"
$resource = "https://analysis.windows.net/powerbi/api"
$authority = "UseTheLogin.Microsoft.Com Here"
$tokenUrl = "MyURL"
# Create variable for access tokens
$body = @{
"resource" = "$resource";
"client_id" = "$appId";
"client_secret" = "$clientSecret";
"grant_type" = "client_credentials"
}
# Pass PBI token thru API service
$tokenResponse = Invoke-RestMethod -Uri $tokenUrl -Method POST -Body $body
# Specify your Power BI username or email as the new owner
$newOwner = "YourNewWorkspaceOwner@domain.com" # Change this
# Connect to Power BI service using the obtained token and credentials
Connect-PowerBIServiceAccount -ServicePrincipal -Credential $credential -TenantId $tenantId -ErrorAction Stop
# Get all workspaces
$allWorkspaces = Get-PowerBIWorkspace -Scope Individual
# Specify the workspace names you want to change ownership for
$workspaceNames = "Analyze Popular Stocks with Power BI"
foreach ($workspaceName in $workspaceNames) {
# Find the workspace by name
$workspace = $allWorkspaces | Where-Object { $_.Name -eq $workspaceName }
if ($workspace -ne $null) {
# Change the owner of the workspace
Set-PowerBIWorkspace -Id $workspace.Id -UserPrincipalName $newOwner
Write-Host "Ownership changed for workspace: $($workspace.Name)"
} else {
Write-Host "Workspace '$workspaceName' not found."
}
}