Skip to content

Instantly share code, notes, and snippets.

@win2000b
Last active July 19, 2019 12:29
Show Gist options
  • Save win2000b/103c259ff25ce7190412f75bb6688e76 to your computer and use it in GitHub Desktop.
Save win2000b/103c259ff25ce7190412f75bb6688e76 to your computer and use it in GitHub Desktop.
Bulk change PowerBI Datasets to new source
# Insert AAD Client ID below
$clientId = "Insert Azure AD Application Client ID In Here."
# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
function GetAuthToken
{
if(-not (Get-Module AzureRm.Profile)) {
Import-Module AzureRm.Profile
}
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$resourceAppIdURI = "https://analysis.windows.net/powerbi/api"
$authority = "https://login.microsoftonline.com/common/oauth2/authorize";
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
$authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
return $authResult
}
# PART 1: Authentication
# ==================================================================
$token = GetAuthToken
Add-Type -AssemblyName System.Net.Http
$temp_path_root = "$PSScriptRoot\pbi-copy-workspace-temp-storage"
# Building Rest API header with authorization token
$auth_header = @{
'Content-Type'='application/json'
'Authorization'=$token.CreateAuthorizationHeader()
}
# Import Spreadsheet detailing Workspaces and Dataset names etc.
$src = Import-CSV "c:\temp\datasets2.csv"
# For each line in the Spreadsheet build the JSON File.
foreach ($line in $src) {
$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 "URL to be Used: $SubmitURL"
# Uncomment the line below when ready to post to the REST API
#Invoke-WebRequest -Uri $SubmitURL -Method POST -Body $JSON
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment