SOLVED

Microsoft Graph and PowerShell - extracting the data into csv

Deleted
Not applicable

Hey all,

 

Hoping someone can point me in the right direction please?

 

I'm messing about connecting up PowerShell (using PnP) to the Microsoft Graph API with a view of returning the data into a csv format.

 

output.PNG


All I'm wanting to achieve is to retrieve the table header information and the results, is there a way to extrapolate that data out and output to a csv file?

 

Many thanks,

 

Steve

2 Replies
best response
Solution

Hi Steve, you can use below powershell script for your need.

 

Connect-PnPOnline -Scopes "User.Read.All","User.ReadBasic.All","Reports.Read.All"
$accesstoken =Get-PnPAccessToken
$apiUrl = "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageFileCounts(period='D7')"
$result = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -Uri $apiUrl -Method Get
#Remove special chars from header
$result = $result.Replace('Report Refresh Date','Report Refresh Date')
#Convert the stream result to an array
$resultarray = ConvertFrom-Csv -InputObject $result
#Export result to CSV
$resultarray | Export-Csv "C:\SiteUsage.csv" -NoTypeInformation
Huge thanks for this Kevin!