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!"
Hi arvindsingh802 ,
Thank you for contacting the Microsoft Fabric Community and sharing the error details.
Given the 403 Forbidden error message and arvindsingh802 insightful input, it seems the issue is linked to authentication or permission configurations, particularly during token creation.
We appreciate arvindsingh802 assistance. To help resolve this promptly, could you please confirm the following.
- Are you using a Service Principal for authentication?
- Have you added the necessary API permissions to your Azure AD App Registration and granted admin consent? Additionally, which API endpoint are you using to assign the workspace to Fabric capacity?
- Does the Service Principal or user account have access to the Fabric capacity?
- Are the workspace IDs listed in the CSV valid and accessible?
Thank you,
Yugandhar.
The 4 points are successfully done but still getting the "Failed to assign workspaces. Error: The remote server returned an error: (401) Unauthorized"