Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from guyinacube/takeover
Browse files Browse the repository at this point in the history
👊 Update Takeover to use Power BI PowerShell CMDLET
  • Loading branch information
guyinacube authored Feb 6, 2019
2 parents a538618 + 247c35f commit 1452599
Showing 1 changed file with 16 additions and 66 deletions.
82 changes: 16 additions & 66 deletions takeover-dataset.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# This sample script calls the Power BI API to progammatically take over a dataset.

# For documentation, please see:
# https://msdn.microsoft.com/en-us/library/mt784651.aspx
# https://docs.microsoft.com/rest/api/power-bi/datasets/takeoveringroup

# Instructions:
# 1. Install PowerShell (https://msdn.microsoft.com/en-us/powershell/scripting/setup/installing-windows-powershell)
# and the Azure PowerShell cmdlets (Install-Module AzureRM)
# and the Power BI PowerShell cmdlets (Install-Module MicrosoftPowerBIMgmt)
# https://docs.microsoft.com/en-us/powershell/power-bi/overview?view=powerbi-ps
# 2. Run PowerShell as an administrator
# 3. Fill in the parameters below
# 4. Change PowerShell directory to where this script is saved
Expand All @@ -17,77 +18,26 @@
$groupId = " FILL ME IN " # the ID of the group (workspace) that hosts the dataset.
$datasetId = " FILL ME IN " # the ID of dataset to rebind

# AAD Client ID
# To get this, go to the following page and follow the steps to provision an app
# https://dev.powerbi.com/apps
# To get the sample to work, ensure that you have the following fields:
# App Type: Native app
# Redirect URL: urn:ietf:wg:oauth:2.0:oob
# Level of access: all dataset APIs
$clientId = " FILL ME IN "

# End Parameters =======================================

# Calls the Active Directory Authentication Library (ADAL) to authenticate against AAD
# Install-Module AzureAD
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
}

# Get the auth token from AAD
$token = GetAuthToken

# Building Rest API header with authorization token
$authHeader = @{
'Content-Type'='application/json'
'Authorization'=$token.CreateAuthorizationHeader()
}

# properly format groups path
$sourceGroupsPath = ""
if ($groupId -eq "me") {
$sourceGroupsPath = "myorg"
} else {
$sourceGroupsPath = "myorg/groups/$groupId"
}
# Login to the Power BI service with your Power BI credentials
Login-PowerBI

# Make the request to bind to a gateway
$uri = "https://api.powerbi.com/v1.0/$sourceGroupsPath/datasets/$datasetId/BindToGateway"
$uri = "groups/$groupId/datasets/$datasetId/Default.TakeOver"

# Try to bind to a new gateway
try {
Invoke-RestMethod -Uri $uri -Headers $authHeader -Method POST -Verbose
} catch {

$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
try {
Invoke-PowerBIRestMethod -Url $uri -Method Post

if($_.Exception.Response.StatusCode.value__ -eq "401")
# Show error if we had a non-terminating error which catch won't catch
if (-Not $?)
{
Write-Host "Error: No access to app workspace."
}
else
{
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host "StatusBody:" $responseBody
$errmsg = Resolve-PowerBIError -Last
$errmsg.Message
}
} catch {

$errmsg = Resolve-PowerBIError -Last
$errmsg.Message
}

0 comments on commit 1452599

Please sign in to comment.