Forum Discussion

Deleted's avatar
Deleted
Nov 09, 2018

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 csv format.

 


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

  • 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
  • Kevin_Morgan's avatar
    Kevin_Morgan
    Iron Contributor

    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
    • Deleted's avatar
      Deleted
      Huge thanks for this Kevin!

Resources