Forum Discussion
PowerShell script to read the workspace ids/workspace name from a CSV and add them to FabricCapacity
- 1 year ago
have you installed MicrosoftPowerBIMgmt module in your PowerShell?
If you are facing issue creating token, try below code with intercative login, make sure your user has capacity Admin and also have Admin access to all listed workapce# Authenticate to Power BI
Connect-PowerBIServiceAccount
# Define your Fabric Capacity ID
$capacityId = "your-fabric-capacity-id"# Path to the CSV file
$csvFilePath = "C:\path\to\your\workspaces.csv"# Read the CSV file (Ensure the CSV has a header like 'WorkspaceId' or 'WorkspaceName')
$workspaces = Import-Csv -Path $csvFilePath# Loop through each workspace
foreach ($workspace in $workspaces) {
# Get Workspace ID (if only workspace name is provided)
if ($workspace.WorkspaceId -eq $null -or $workspace.WorkspaceId -eq "") {
$workspaceInfo = Get-PowerBIWorkspace -Name $workspace.WorkspaceName
if ($workspaceInfo) {
$workspaceId = $workspaceInfo.Id
} else {
Write-Host "Workspace '$($workspace.WorkspaceName)' not found!" -ForegroundColor Red
continue
}
} else {
$workspaceId = $workspace.WorkspaceId
}# Add the workspace to the Fabric Capacity
$url = "https://api.powerbi.com/v1.0/myorg/capacities/$capacityId/workspaces/$workspaceId"# Invoke REST API to assign the workspace
$response = Invoke-PowerBIRestMethod -Url $url -Method Post# Check if the operation was successful
if ($response) {
Write-Host "Successfully added workspace '$workspaceId' to Fabric Capacity" -ForegroundColor Green
} else {
Write-Host "Failed to add workspace '$workspaceId' to Fabric Capacity" -ForegroundColor Red
}
}Write-Host "Process completed!"
You can use below script
# Parameters
$csvFilePath = "C:\Path\To\Your\WorkspaceList.csv" # Update the path
$capacityId = "your-fabric-capacity-id" # Update with your Fabric capacity ID
# Authenticate to Power BI (use service principal or interactive login)
$clientId = "your-client-id"
$tenantId = "your-tenant-id"
$clientSecret = "your-client-secret"
$body = @{
resource = "https://graph.microsoft.com/"
client_id = $clientId
client_secret = $clientSecret
grant_type = "client_credentials"
scope = "https://graph.microsoft.com/.default"
}
$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" -Method Post -ContentType "application/x-www-form-urlencoded" -Body $body
$accessToken = $tokenResponse.access_token
# Read CSV File
$workspaces = Import-Csv -Path $csvFilePath
# Function to get workspace ID from workspace name
function Get-WorkspaceId {
param ($workspaceName)
$url = "https://api.powerbi.com/v1.0/myorg/groups"
$headers = @{Authorization = "Bearer $accessToken"}
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
$workspace = $response.value | Where-Object { $_.name -eq $workspaceName }
return $workspace.id
}
# Iterate through workspaces
foreach ($workspace in $workspaces) {
$workspaceId = $workspace.WorkspaceId
# If WorkspaceId is empty, fetch it using WorkspaceName
if (-not $workspaceId -and $workspace.WorkspaceName) {
$workspaceId = Get-WorkspaceId -workspaceName $workspace.WorkspaceName
}
if ($workspaceId) {
$assignUrl = "https://api.powerbi.com/v1.0/myorg/capacities/$capacityId/workloads/assign"
$assignBody = @{
groupId = $workspaceId
} | ConvertTo-Json -Depth 1
$assignHeaders = @{
Authorization = "Bearer $accessToken"
"Content-Type" = "application/json"
}
try {
$response = Invoke-RestMethod -Uri $assignUrl -Headers $assignHeaders -Method Post -Body $assignBody
Write-Output "Successfully added Workspace ID $workspaceId to Fabric capacity."
} catch {
Write-Output "Failed to add Workspace ID $workspaceId. Error: $_"
}
} else {
Write-Output "Workspace not found: $($workspace.WorkspaceName)"
}
}
Thanks for providing however I am getting attached error.
- arvindsingh8021 year agoCommunity Champion
have you installed MicrosoftPowerBIMgmt module in your PowerShell?
If you are facing issue creating token, try below code with intercative login, make sure your user has capacity Admin and also have Admin access to all listed workapce# Authenticate to Power BI
Connect-PowerBIServiceAccount
# Define your Fabric Capacity ID
$capacityId = "your-fabric-capacity-id"# Path to the CSV file
$csvFilePath = "C:\path\to\your\workspaces.csv"# Read the CSV file (Ensure the CSV has a header like 'WorkspaceId' or 'WorkspaceName')
$workspaces = Import-Csv -Path $csvFilePath# Loop through each workspace
foreach ($workspace in $workspaces) {
# Get Workspace ID (if only workspace name is provided)
if ($workspace.WorkspaceId -eq $null -or $workspace.WorkspaceId -eq "") {
$workspaceInfo = Get-PowerBIWorkspace -Name $workspace.WorkspaceName
if ($workspaceInfo) {
$workspaceId = $workspaceInfo.Id
} else {
Write-Host "Workspace '$($workspace.WorkspaceName)' not found!" -ForegroundColor Red
continue
}
} else {
$workspaceId = $workspace.WorkspaceId
}# Add the workspace to the Fabric Capacity
$url = "https://api.powerbi.com/v1.0/myorg/capacities/$capacityId/workspaces/$workspaceId"# Invoke REST API to assign the workspace
$response = Invoke-PowerBIRestMethod -Url $url -Method Post# Check if the operation was successful
if ($response) {
Write-Host "Successfully added workspace '$workspaceId' to Fabric Capacity" -ForegroundColor Green
} else {
Write-Host "Failed to add workspace '$workspaceId' to Fabric Capacity" -ForegroundColor Red
}
}Write-Host "Process completed!"
- NBK521 year agoHelper I