Last active
December 3, 2023 23:45
-
-
Save dgosbell/466edf17bac2cf02e888095f84d52baa to your computer and use it in GitHub Desktop.
Moves all Power BI / Fabric workspaces from one premium capacity to another. Useful for when you want to migrate between a P SKU and an F SKU and cannot simply scale up/down the existing SKU
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Modules MicrosoftPowerBIMgmt | |
<# | |
.SYNOPSIS | |
This script will move all workspace in one capacity to another capacity | |
.DESCRIPTION | |
.NOTES | |
File Name : Move-PowerBICapacityWorkspaces.ps1 | |
Author : Darren Gosbell | |
Date : 13 Oct 2023 | |
Description: | |
.EXAMPLE | |
Move-PowerBICapacityWorkspaces -fromCapacityName "pbixmla" -toCapacityName "myf2" | |
#> | |
function Move-PowerBICapacityWorkspaces ($fromCapacityName, $toCapacityName) | |
{ | |
# check if we are logged in and if not login to Power BI | |
try { | |
get-powerbiaccesstoken | Out-Null | |
} | |
catch { | |
Login-PowerBI | out-null | |
} | |
# Get a list of all available capacities | |
$allCapacities = Get-PowerBICapacity | |
# Get the "From" capacity details | |
$fromCapacity = ($allCapacities | where {$_.DisplayName -eq "$fromCapacityName"} ) | |
if ($fromCapacity -eq $null) { throw "Unable to find a capacity with the display name of: $fromCapacityName`n"} | |
if ($fromCapacity.State -ne "Active") {throw "The capacity $($fromCapacityName) must be active in order to move workspaces`n"} | |
$fromCapacityId = $fromCapacity.Id.ToString() | |
# get the "To" capacity details | |
$toCapacity = ($allCapacities | where {$_.DisplayName -eq "$toCapacityName"} ) | |
if ($toCapacity -eq $null) { throw "Unable to find a capacity with the display name of: $toCapacityName`n"} | |
if ($toCapacity.State -ne "Active") {throw "The capacity $($toCapacityName) must be active in order to move workspaces`n"} | |
$toCapacityId = $toCapacity.Id.ToString() | |
if ($fromCapacity.Region -ne $toCapacity.Region) {throw "You cannot move workspaces to a capacity in a different region`nFrom: $($fromCapacity.DisplayName) : $($fromCapacity.Region)`n To: $($toCapacity.DisplayName) : $($toCapacity.Region)`n"} | |
$workspaces = @(Get-PowerBIWorkspace -Filter "capacityId eq toupper('$fromCapacityId')" ) | |
write-host "Moving $(($workspaces.Count)) workspace(s)" -ForegroundColor Cyan | |
write-host "From capacity '$fromCapacityName' to '$toCapacityName'" -ForegroundColor Cyan | |
$errorCnt = 0 | |
foreach ($workspace in $workspaces) | |
{ | |
">> Moving Workspace: $($workspace.name) - $($workspace.Id)" | |
try { | |
$result = Invoke-PowerBIRestMethod -Method Post -Url "https://api.powerbi.com/v1.0/myorg/groups/$($workspace.Id)/AssignToCapacity" -Body "{`"capacityId`": `"$toCapacityId`"}" -ErrorAction stop | |
} | |
catch | |
{ | |
$errorCnt = $errorCnt + 1 | |
throw $_.Exception.InnerException | |
} | |
} | |
if ($errorCnt -eq 0) { | |
Write-Host -ForegroundColor Green "Capacity Assignment Complete" | |
} | |
else | |
{ | |
Write-Host "$errorCnt error(s) occurred while trying to move $($workspaces.count) workspace(s)" -ForegroundColor Red | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment