I created an Azure function with the language PowerShell. I would like to use the Power Bi Rest Api. What is the best way to do the authentcation from an Azure function? Can I use for example managed identity or an Azure app registration?
I would like to run for example:
https://api.powerbi.com/v1.0/myorg/admin/dataflows
I would like to run a seperated PowerShell script to get data from Power Bi.
You can make use of Login-PowerBIServiceAccount
to connect to Power Bi:
Login-PowerBIServiceAccount
Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/admin/dataflows' -Method GET
Or if you want to make use of managed identity or App registration, use the below PowerShell script:
Note that: My Workspace isn't supported when using service principal that is myorg and works for only - Service principal only works with new workspaces. Refer this MsDoc
Create an Azure AD application and grant application API permissions:
Add the service principal as admin to the workspace:
Go to Power BI Portal -> Your Workspace -> Click on ... -> Manage access -> +Add people or groups -> Search for Service Principal with name -> Admin
Enable "Allow access for service principals to call Power BI APIs" in the Power Bi Admin portal:
# Replace these values with your own
$tenantId = "TenantID"
$appId = "AppID"
$appSecret = "ClientSecret"
# Authenticate with Azure AD using the App Registration
$authUrl = "https://login.microsoftonline.com/TenantID/oauth2/v2.0/token"
$body = @{
grant_type = "client_credentials"
client_id = $appId
client_secret = $appSecret
scope = "https://analysis.windows.net/powerbi/api/.default"
}
$authResult = Invoke-WebRequest -Uri $authUrl -Method POST -Body $body
$accessToken = ($authResult.Content | ConvertFrom-Json).access_token
# Call the Power BI REST API to retrieve a list of dataflows
$Url = "https://api.powerbi.com/v1.0/myorg/groups/WorkSpaceID/dataflows"
$headers = @{
Authorization = "Bearer $accessToken"
}
$Result = Invoke-WebRequest -Uri $Url -Method GET -Headers $headers
$data = ($Result.Content | ConvertFrom-Json).value
# Output the list of dataflows
$data