Import-Module powershell-yaml
$AccessToken = @{Authorization='Bearer ' + (Invoke-WebRequest -Uri https://login.microsoftonline.com/common/oauth2/v2.0/token -Method Post -Body $AccessTokenParams | ConvertFrom-Json).access_token}
$datasetIdList = ((((Invoke-WebRequest -Uri https://api.powerbi.com/v1.0/myorg/groups/{groupid}/reports -Method Get -Headers $AccessToken).Content) | ConvertFrom-Yaml).value).datasetId
foreach ($dataset in $datasetIdList)
{
$datasource = ((Invoke-WebRequest -Uri https://api.powerbi.com/v1.0/myorg/groups/{groupid}/datasets/$dataset/datasources -Method Get -Headers $AccessToken).Content)
if (-Not ($datasource -like "*unwantedsourcetochange1*") -And -Not ($datasource -like "*unwantedsourcetochange2*") -And -Not ([string]::IsNullOrEmpty(($datasource | ConvertFrom-Yaml).value))) {
$datasource = $datasource.replace('oldstring','newstring')
$updateDetails= @()
$connectionList = ($datasource | ConvertFrom-Yaml).value.connectionDetails
foreach ($connection in $connectionList){
$server = $connection.server
$path = $connection.path
$connectionObject = [PSCustomObject]@{
"server" = $server
"path" = $path
}
$datasourceSelector = [PSCustomObject]@{
"datasourceSelector" = @{
"datasourceType" = "AzureDataLakeStorage"
"connectionDetails" = $connectionObject
}
}
$connectionDetails = [PSCustomObject]@{
"connectionDetails" = $connectionObject
}
$updateDetails += $datasourceSelector
$updateDetails += $connectionDetails
}
$json = [PSCustomObject]@{
"updateDetails" = @($updateDetails)
}
$json = $json | ConvertTo-Json -Depth 100
if ($dataset -eq "specific-datasetid-i-want-to-change-for-testing-purposes") {
Invoke-WebRequest -Uri https://api.powerbi.com/v1.0/myorg/groups/{groupid}/datasets/{specific datasetid}/Default.UpdateDatasources -Method Post -Body $json -Headers $AccessToken
}
}
}