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

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
Deepa
  • 19
  • 4
  • use ```export-csv``` to generate a csv: ```$reports | export-csv "D:\2709\PS4.csv"``` – Toni Oct 04 '22 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"`? – iRon Oct 04 '22 at 10:54
  • 1
    See: [Convert nested JSON array into separate columns in CSV file](https://stackoverflow.com/a/46081131/1701026) – iRon Oct 04 '22 at 11:49

1 Answers1

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.

Toni
  • 1,738
  • 1
  • 3
  • 11
  • Hi, thank you for answering, I did try export-csv earlier, but I got the output as below. Length 9697 – Deepa Oct 04 '22 at 10:57
  • Yes, this is giving me system.object[] , not the values inside. odata.context odata.count value http://wabi-us-east2-c-primary-redirect.analysis.windows.net/v1.0/myorg/admin/$metadata#reports 33954 System.Object[] – Deepa Oct 04 '22 at 11:25
  • yes as expected. iRon provided a link above showing how flattern the structure... – Toni Oct 04 '22 at 11:50