Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jsnape
Regular Visitor

HTTP 400 Bad Request calling Datasets - Update Datasources In Group REST method

I am writing a script that users can run to update all their DNS based analysis services connections to the new link:// alias style connection. The whole script is below:

 

#Requires -Version 5
Set-StrictMode -Version Latest

Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser
Import-Module MicrosoftPowerBIMgmt
Connect-PowerBIServiceAccount

try {
    $groups = Invoke-PowerBiRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/groups/' -Method GET | ConvertFrom-Json

    foreach ($group in $groups.value) {
        Write-Host "Processing group $($group.name)"
        $groupId = $group.id
        $dataSets = Invoke-PowerBiRestMethod -Url "https://api.powerbi.com/v1.0/myorg/groups/$groupId/datasets" -Method GET | ConvertFrom-Json

        foreach ($dataset in $datasets.value) {
            Write-Host "Processing dataset $($dataset.name)"
            $dataSetId = $dataset.id

            $dataSourceUri = "https://api.powerbi.com/v1.0/myorg/groups/$groupId/datasets/$dataSetId/datasources"
            $dataSources = Invoke-PowerBiRestMethod -Url $dataSourceUri -Method GET | ConvertFrom-Json

            if (-not $dataSources) {
                Resolve-PowerBIError -Last
                continue
            }

            $updates = $dataSources.value |
                Where-Object { $_.datasourceType -eq 'AnalysisServices' } |
                Where-Object { $_.connectionDetails.server -match 'cube\.ds-\w*\.foobar\.com' } |
                ForEach-Object {
                    $update = [pscustomobject] [ordered] @{
                        datasourceSelector = [pscustomobject] [ordered] @{
                            datasourceType = $_.datasourceType
                            connectionDetails = $_.connectionDetails
                        }
                        connectionDetails = [pscustomobject] [ordered] @{
                            server = "link://wibble.foobar.com"
                            database = $_.connectionDetails.database
                        }
                    }

                    Write-Output $update
                }

            if ($null -eq $updates)
            {
                continue
            }

            Write-Host "Found analysis services connections"

            $updateDetails = @{
                updateDetails = $updates
            }

            Write-Host "Updating connection details" -ForegroundColor Cyan

            $body = $updateDetails | ConvertTo-Json -Depth 10
            $url = "https://api.powerbi.com/v1.0/myorg/groups/$groupId/datasets/$dataSetId/Default.UpdateDatasources"
            $updateResponse = Invoke-PowerBiRestMethod -Url $url -Method POST -Body $body -ErrorAction Stop | ConvertFrom-Json

            Write-Output $updateResponse
        }
    }
}
catch {
    Resolve-PowerBIError -Last
}

The final post body looks fine according to the docs but I get a Bad Request response from the service.

 

With no additional clues I have no idea what to try next.

1 REPLY 1
Anonymous
Not applicable

Did you have a resolution for this issue?

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.