Forum Discussion

DWD_76's avatar
DWD_76
Copper Contributor
Jul 05, 2022

Help with Power BI REST API output

I have the following script that is using Invoke-PowerBIRestMethod.  I have a couple of issues I need to help solving. 

 

1. You can see I'm trying to convert the output of the API to a table or some kind of useable/readable format.  Getting it to a csv would be fine but none of the output is useful or formatted. 

2. The output does not include the Original GUIDs that I am looping through.  Is there a way to get those added to the final output. Somerhing like 

 

GUID - (The actual report id uses in the API url parameter) 

emailaddress

owner

displayname

identifier

userType

 

 

Login-PowerBIServiceAccount

 

$ReportIDs = $(Get-PowerBIReport -Scope Organization).ID
$ReportIDs

$endreport = @()

ForEach ($id in $ReportIDs){

 

$endreport +=
--Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/admin/reports/$id/users" ----Method Get|ConvertFrom-String|Format-List
#the convert and format-tables don't really help when I try to export them.

 

}


$endreport

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    DWD_76 

     

    I can't help with any live testing as I don't have PowerBI access, but running a search on a suspicion I had confirms that the results are in JSON form, meaning you're ConvertFrom-String should actually be ConvertFrom-Json.

     

    Have a read of this old Microsoft blog for a broader overview.

     

    I've also included the commandlet reference but it's not overly helpful from an examples perspective.

     

     

    Since I can't test against PowerBI, I'll offer a generic guide below on how to include the report GUID but you'll have to work on this yourself - unless someone else here has access and can do some testing for you.

     

    In relation to the API reference (below), I can't tell if properties like principalType and reportUserAccessRight are single- or multi-valued. I've assumed the former but you'll be able to see that better than I.

     

     

    This only relates to your Invoke-PowerBIRestMethod call. What you do before, after and with it is up to you.

     

    Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/admin/reports/$id/users" -Method Get |
        ConvertFrom-Json |
            ForEach-Object {
                [PSCustomObject] @{
                    ReportId = $id;
                    PrincipalType = $_.principalType;
                    Identifier = $_.identifier;
                    Owner = $_.reportUserAccessRight -eq "Owner";
                    DisplayName = $_.displayName;
                    EmailAddress = $_.emailAddress;
                }
            }

     

    Cheers,

    Lain

Resources