Created
February 19, 2024 18:53
-
-
Save neerajks77/7b946c81480763b7c9590160f7e088aa to your computer and use it in GitHub Desktop.
This script is used to create OneDrive Monitoring and governance reports
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################################### | |
######Author: Neeraj Kumar | |
######Created: 28/09/2023 | |
######Usage: This script is used to create OneDrive Monitoring and governance reports. Please create required variables under shared resources. | |
##################################################################################### | |
$global:tenantId = Get-AutomationVariable -Name 'TenantId' | |
$global:clientId = Get-AutomationVariable -Name 'ClientId' | |
$global:clientSecret = Get-AutomationVariable -Name 'ClientSecret' | |
$global:certThumbprint = Get-AutomationVariable -Name 'CERT_THUMBPRINT' | |
$global:spSiteUrl = Get-AutomationVariable -Name 'SharePointSiteUrl' | |
$global:spSiteName = Get-AutomationVariable -Name 'SharePointSiteName' | |
$global:spLibraryName = Get-AutomationVariable -Name 'OneDriveDocumentLibraryName' | |
$global:accessToken =$null | |
$global:DestinationURL ="$spSiteUrl/sites/$spSiteName/$spLibraryName" | |
<# Connect to Graph API and get access tokens #> | |
Function ConnectToGraph() | |
{ | |
try | |
{ | |
Connect-MgGraph -ClientId $global:clientId -TenantID $global:TenantId -CertificateThumbprint $global:certThumbprint -Nowelcome | |
$graphtokenBody = @{ | |
Grant_Type = "client_credentials" | |
Client_Id = $clientId | |
Client_Secret = $clientSecret | |
Scope = "https://graph.microsoft.com/.default" | |
} | |
$jsonBody = $graphtokenBody #| ConvertTo-Json | |
$oauth = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" -Body $jsonBody | |
$global:accessToken = @{'Authorization'="$($oauth.token_type) $($oauth.access_token)"} | |
} | |
catch { | |
Write-Error $Error[0] | |
Write-Output "Error in connecting to Microsoft Graph" | |
$Error=$null | |
} | |
} | |
<# 2. Connect to SharePoint Online #> | |
function ConnectToSharePoint | |
{ | |
$SiteUrl = "$global:spSiteUrl/sites/$global:spSiteName" | |
Write-Output $SiteUrl | |
try | |
{ | |
$SiteMemberCredential = Get-AutomationPSCredential -Name "SharePoint" | |
Connect-PnPOnline -Url $SiteUrl -Credentials $SiteMemberCredential | |
} | |
catch{Write-Host "Unable to connect to SharePoint Online.." | |
Write-Error Error[0] | |
Error=$null | |
} | |
} | |
<# 3. Execute Graph API Reports for Exchange #> | |
function ExecuteReportAPI | |
{ | |
param([String[]] $Param ) | |
$CSVFileName = $Param[0] | |
$graphApiUri = $Param[1] | |
$filePath = $env:Temp | |
try{ | |
Write-Output "Fetching report output by executing Graph API" | |
$Reports = Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $global:accessToken | ConvertFrom-Csv | |
$Reports | Export-Csv -Path $filePath\$CSVFileName -NoTypeInformation | |
WritetoSharePoint($CSVFileName) | |
} | |
catch | |
{ Write-Output "Exception while fetching Usage report"} | |
} | |
# Output results to SharePoint folder | |
function WritetoSharePoint{ | |
param( $CSVFileName ) | |
# Export to CSV locally | |
$filePath = $env:Temp | |
# Upload to SharePoint | |
$FolderObject = Get-PnPFolder -Url $global:DestinationURL | |
$Upload= Add-PnPFile -Path $filePath\$CSVFileName -Folder $FolderObject | |
If ($Upload -ne $null) | |
{ | |
Write-Output $CSVFileName " Report sucessfully uploaded" | |
} | |
# Clean up local file | |
Remove-Item -Path $filePath\$CSVFileName -Force | |
} | |
function main | |
{ | |
ConnectToGraph | |
ConnectToSharePoint | |
ExecuteReportAPI("1.OneDriveUsageFileCounts_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveUsageFileCounts(period='D30')") | |
ExecuteReportAPI("1.OneDriveStorage_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveUsageStorage(period='D30')") | |
ExecuteReportAPI("1.OneDriveUsageAccountDetail_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveUsageAccountDetail(period='D30')") | |
ExecuteReportAPI("2.OneDriveActivityUserDetail_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveActivityUserDetail(period='D30')") | |
ExecuteReportAPI("2.OneDriveActivityUserCounts_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveActivityUserCounts(period='D30')") | |
ExecuteReportAPI("2.OneDriveActivityFileCounts_30.csv","https://graph.microsoft.com/v1.0/reports/getOneDriveActivityFileCounts(period='D30')") | |
} | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment