0

I have recently started using powershell and struggling to achieve something. I am calling an API in my powershell script, and the output is a list of reports, where each report has a set of properties like reportID, reportname, workspaceId etc. I need to export this entire output to csv in a proper table format, and also export only one property (workspaceId) of each report to another csv file.

If someone is aware of how to do this, please help.

Below is a part of my script:

$reports = Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/admin/reports?$top=10' -Method Get
$reports | Out-File -Path "D:\2709\PS4.csv"

output

3
  • use export-csv to generate a csv: $reports | export-csv "D:\2709\PS4.csv" Commented Oct 4, 2022 at 10:48
  • Invoke-PowerBIRestMethod -Url 'https://api.powerbi.com/v1.0/myorg/admin/reports?$top=10' -Method Get |export-csv "D:\2709\PS4.csv"? Commented Oct 4, 2022 at 10:54
  • 1
    See: Convert nested JSON array into separate columns in CSV file Commented Oct 4, 2022 at 11:49

1 Answer 1

0
#to export all properties
$reports | export-csv "D:\2709\PS4.csv"

#to export a selection of properties
$reports | select-object -property workspaceId  | export-csv "D:\2709\PS4.csv"

ok I see, $reports contains a json. in principle you can convert the json to csv by doing this:

ConvertFrom-Json -InputObject $reports | export-csv "D:\2709\PS4.csv"

But this gives you only a usefull output if the structure is flat. If thats not the case we have to flattern the structure first before we can export it as csv.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thank you for answering, I did try export-csv earlier, but I got the output as below. Length 9697
Yes, this is giving me system.object[] , not the values inside. odata.context odata.count value wabi-us-east2-c-primary-redirect.analysis.windows.net/v1.0/… 33954 System.Object[]
yes as expected. iRon provided a link above showing how flattern the structure...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.