Forum Discussion

Anonymous's avatar
Anonymous
Nov 09, 2018
Solved

Microsoft Graph and PowerShell - extracting the data into csv

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 ...
  • Kevin_Morgan's avatar
    Nov 10, 2018

    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

Resources