microsoft / microsoft/powerbi-powershell

Invoke-PowerBIRestMethod fails with Service Principal

Open
#108 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
413
Forks
134
Avg merge
33m
Merged PRs (30d)
1

Description

Invoke-PowerBIRestMethod does not function when being called from a Service Principal. I have configured the service principal in the Azure tenant with the required API permissions, but the service account can't call the Invoke-PowerBIRestMethod. The value returned says it's not authorized even though it has permissions. I also tried this with Get-PowerBIReport -Scope Organization and Invoke-RestMethod with no success.

Example script:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$tenantId = "#####"
$AppId = "#####"
$PasswordClearText = "######"

$PW = ConvertTo-SecureString -String $PasswordClearText -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $AppId, $PW

Import-Module -name MicrosoftPowerBIMgmt

$Account = Connect-PowerBIServiceAccount -Credential $Credential -Tenant $tenantId -ServicePrincipal
if(!$Account) {
Throw "Could not connect to Power BI service account."
}

$Reportsresponse = Invoke-PowerBIRestMethod -Url 'reports' -Method Get
if(!$Reportsresponse){
Resolve-PowerBIError
}

$Reportsresponse.value | Out-Host

Disconnect-PowerBIServiceAccount

Message : Response status code does not indicate success: 401 (Unauthorized).
StackTrace : at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
at Microsoft.PowerBI.Commands.Profile.InvokePowerBIRestMethod.d__31.MoveNext()
Exception : System.Net.Http.HttpRequestException
InvocationInfo : {Invoke-PowerBIRestMethod}
Line : $Reportsresponse = Invoke-PowerBIRestMethod -Url 'reports' -Method Get

Position : - Doc\PowerShell\Azure Authentication Test - Az.ps1:46 char:20
+ ... Reportsresponse = Invoke-PowerBIRestMethod -Url 'reports' -Method Get
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId : 1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the example script, especially Connect-PowerBIServiceAccount and Invoke-PowerBIRestMethod -Url 'reports' -Method Get, and reproduce the 401 response. Inspect the cmdlet's service-principal authentication path and its existing tests or request handling. Done means the documented service-principal setup can successfully retrieve reports through Invoke-PowerBIRestMethod.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
api, authentication
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.