11# This sample script calls the Power BI API to progammatically take over a dataset.
22
33# For documentation, please see:
4- # https://msdn .microsoft.com/en-us/library/mt784651.aspx
4+ # https://docs .microsoft.com/rest/api/power-bi/datasets/takeoveringroup
55
66# Instructions:
77# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell)
8- # and the Azure PowerShell cmdlets (Install-Module AzureRM)
8+ # and the Power BI PowerShell cmdlets (Install-Module MicrosoftPowerBIMgmt)
9+ # https://docs.microsoft.com/en-us/powershell/power-bi/overview?view=powerbi-ps
910# 2. Run PowerShell as an administrator
1011# 3. Fill in the parameters below
1112# 4. Change PowerShell directory to where this script is saved
1718$groupId = " FILL ME IN " # the ID of the group (workspace) that hosts the dataset.
1819$datasetId = " FILL ME IN " # the ID of dataset to rebind
1920
20- # AAD Client ID
21- # To get this, go to the following page and follow the steps to provision an app
22- # https://dev.powerbi.com/apps
23- # To get the sample to work, ensure that you have the following fields:
24- # App Type: Native app
25- # Redirect URL: urn:ietf:wg:oauth:2.0:oob
26- # Level of access: all dataset APIs
27- $clientId = " FILL ME IN "
28-
2921# End Parameters =======================================
3022
31- # Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
32- # Install-Module AzureAD
33- function GetAuthToken
34- {
35- if (-not (Get-Module AzureRm.Profile)) {
36- Import-Module AzureRm.Profile
37- }
38-
39- $redirectUri = " urn:ietf:wg:oauth:2.0:oob"
40-
41- $resourceAppIdURI = " https://analysis.windows.net/powerbi/api"
42-
43- $authority = " https://login.microsoftonline.com/common/oauth2/authorize" ;
44-
45- $authContext = New-Object " Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" - ArgumentList $authority
46-
47- $authResult = $authContext.AcquireToken ($resourceAppIdURI , $clientId , $redirectUri , " Auto" )
48-
49- return $authResult
50- }
51-
52- # Get the auth token from AAD
53- $token = GetAuthToken
54-
55- # Building Rest API header with authorization token
56- $authHeader = @ {
57- ' Content-Type' = ' application/json'
58- ' Authorization' = $token.CreateAuthorizationHeader ()
59- }
60-
61- # properly format groups path
62- $sourceGroupsPath = " "
63- if ($groupId -eq " me" ) {
64- $sourceGroupsPath = " myorg"
65- } else {
66- $sourceGroupsPath = " myorg/groups/$groupId "
67- }
23+ # Login to the Power BI service with your Power BI credentials
24+ Login- PowerBI
6825
6926# Make the request to bind to a gateway
70- $uri = " https://api.powerbi.com/v1.0/ $sourceGroupsPath /datasets/$datasetId /BindToGateway "
27+ $uri = " groups/ $groupId /datasets/$datasetId /Default.TakeOver "
7128
7229# Try to bind to a new gateway
73- try {
74- Invoke-RestMethod - Uri $uri - Headers $authHeader - Method POST - Verbose
75- } catch {
76-
77- $result = $_.Exception.Response.GetResponseStream ()
78- $reader = New-Object System.IO.StreamReader($result )
79- $reader.BaseStream.Position = 0
80- $reader.DiscardBufferedData ()
81- $responseBody = $reader.ReadToEnd ();
30+ try {
31+ Invoke-PowerBIRestMethod - Url $uri - Method Post
8232
83- if ($_.Exception.Response.StatusCode.value__ -eq " 401" )
33+ # Show error if we had a non-terminating error which catch won't catch
34+ if (-Not $? )
8435 {
85- Write-Host " Error: No access to app workspace."
86- }
87- else
88- {
89- Write-Host " StatusCode:" $_.Exception.Response.StatusCode.value__
90- Write-Host " StatusDescription:" $_.Exception.Response.StatusDescription
91- Write-Host " StatusBody:" $responseBody
36+ $errmsg = Resolve-PowerBIError - Last
37+ $errmsg.Message
9238 }
39+ } catch {
40+
41+ $errmsg = Resolve-PowerBIError - Last
42+ $errmsg.Message
9343}
0 commit comments