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!"
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!"