Skip to content

Instantly share code, notes, and snippets.

@win2000b
Last active October 24, 2022 16:38
Show Gist options
  • Save win2000b/f56b87c537e82ee9ec8f173914937d70 to your computer and use it in GitHub Desktop.
Save win2000b/f56b87c537e82ee9ec8f173914937d70 to your computer and use it in GitHub Desktop.
Take OwnerShip of PowerBI Dataset, Change Datasource and then output result
# Connect to PowerBI Service In PowerShell
Connect-PowerBIServiceAccount
# Import the Data File for all the PowerBI Datasets you want to change.
$src = Import-CSV "c:\temp\DataImport.csv"
foreach ($line in $src) {
$GetURL = "https://api.powerbi.com/v1.0/myorg/groups/$($line.WorkSpaceID)/datasets/$($line.DatasetID)/datasources"
$TakeOwnURL = "https://api.powerbi.com/v1.0/myorg/groups/$($line.WorkSpaceID)/datasets/$($line.DatasetID)/Default.TakeOver"
$SubmitURL = "https://api.powerbi.com/v1.0/myorg/groups/$($line.WorkSpaceID)/datasets/$($line.DatasetID)/Default.UpdateDatasources"
$obj = [PSCustomObject]@{"updateDetails" = @(@{"datasourceSelector" = @{"datasourceType" = "AnalysisServices"; "connectionDetails" = @{"server" = $($line.serverOLD); "database" = $($line.databaseold)}}; "connectionDetails" = @{"server" = $($line.serverNEW); "database" = $($line.databaseNEW)}})}
$JSON = $obj | ConvertTo-JSON -Depth 10
# Comment out the lines below once testing complete
Write-Host $JSON
Write-Host "Submit URL to be Used: $SubmitURL"
Write-Host "Takeown URL to be Used: $TakeOwnURL"
# Uncomment out the lines below when ready to implement.
# Invoke-PowerBIRestMethod -Url $TakeOwnURL -Method Post
# Invoke-PowerBIRestMethod -Url $SubmitURL -Method POST -Body $JSON
# Invoke-PowerBIRestMethod -Url $GetURL -Method GET
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment