Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

How to access the PowerBI report using Azure Automation Powershell Runbook

Hi,

My requirement is to access a PowerBI Report using the Azure Automation Powershell Runbook. Have tried couple of ways to acheive it, but was not successful and have provided them below. Can you please suggest.

 

- Created App registration for PowerBI in Azure Active directory.

- Have used the below code in Azure Powershell runbook to invoke the REST API of the PowerBI.

Code: 

   $connection = Get-AutomationConnection -Name 'AzureRunAsConnection'

   Connect-AzAccount -ServicePrincipal -Tenant $connection.TenantID -ApplicationId $connection.ApplicationID -CertificateThumbprint $connection.CertificateThumbprint

 

$tenantDomain = 'XXXXXXXXXXXX.net'
$TenantId='XXXXXXX-XXXX-XXX-XXX-9ba90260bca6'
$ApplicationClientId = 'XXXXXXXX-XXXX-XXXX-XXXX-21d9e848ebad'
$ApplicationClientSecretKey = 'XXX-X~XXXXXXXXX-XXX-XXX~XX~12ReGrs' 
 
[uri]$url =  'https://app.powerbi.com/'
Write-Host "Authorization..." -ForegroundColor Yellow
Add-Type -AssemblyName System.Web
[string]$absoluteURL = $url.AbsoluteUri.Remove($url.AbsoluteUri.Length-1,1)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Write-Host "1"

$Body = @{
    "client_id" = $ApplicationClientId
    "client_secret" = $ApplicationClientSecretKey
    "grant_type" = 'client_credentials'
    "scope" = "$absoluteURL/.default"
}
Write-Host "2"
Write-Host "   URL" $absoluteURL -ForegroundColor Yellow
Write-Host "   Body" -ForegroundColor Yellow
$Body
Write-Host "   Trying to get authorization..." -ForegroundColor Yellow
Write-Host "3"

$login = $null
$login = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantDomain/oauth2/v2.0/token" -Body $Body -ContentType 'application/x-www-form-urlencoded' -Verbose
$login

$Bearer = $null
[string]$Bearer = $login.access_token

$headers = @{
    "Accept" = "application/xml"
    "Accept-Charset" = "UTF-8"
    "Authorization" = "Bearer $Bearer"
    "Host" = "$($url.Host)"
}
[System.UriBuilder] $ListRecordsURL = $url

 $resultREST = Invoke-RestMethod -Method Get -Uri $ListRecordsURL.Uri.AbsoluteUri `
        -Headers $headers -ContentType 'application/json; charset=utf-8' -Verbose
 $resultREST
 
Getting the below Error:
 

 

 Thanks & Regards
Srujana U

1 Reply