Skip to content

Instantly share code, notes, and snippets.

@neerajks77
Last active February 19, 2024 19:01
Show Gist options
  • Save neerajks77/634ed80ca5c4c730b499bbc2b5bc6673 to your computer and use it in GitHub Desktop.
Save neerajks77/634ed80ca5c4c730b499bbc2b5bc6673 to your computer and use it in GitHub Desktop.
Updated - Extract the details of the SharePoint Online Sites that includes Site URL, Administrator Email, Group Email Id, Storage Space Used, and User Count - Updated
#####################################################################################
######Author: Neeraj Kumar
######Created: 28/09/2023
######Usage: Extract the details of Microsoft SharePoint that includes Site Team and channel details, storage space, owner details, etc. Please create required variables under shared resources.
#####################################################################################
# Variables from Azure Automation Account
$tenantId = Get-AutomationVariable -Name 'TenantId'
$clientId = Get-AutomationVariable -Name 'ClientId'
$clientSecret = Get-AutomationVariable -Name 'ClientSecret'
$spSiteUrl = Get-AutomationVariable -Name 'SharePointSiteUrl'
$spSiteName = Get-AutomationVariable -Name 'SharePointSiteName'
$spLibraryName = Get-AutomationVariable -Name 'SharePointDocumentLibraryName'
# Function to get token
function ConnecttoGraph {
$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$tokenBody = @{
client_id = $clientId
scope = "https://graph.microsoft.com/.default"
client_secret = $clientSecret
grant_type = "client_credentials"
}
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUrl -Body $tokenBody
$global:accessToken = @{'Authorization'="$($tokenResponse.token_type) $($tokenResponse.access_token)"}
write-output $global:accessToken
}
function GetSharePointDetails{
# $headers = @{
# "Authorization" = "Bearer $Global:tokenResponse"
# "Content-Type" = "application/json"
# }
# Get all SharePoint Site Collections
try
{
$spSites = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D30')" | ConvertFrom-Csv
$Global:spLists = @()
$Global:SPDetails = @()
foreach ($spSite in $spSites)
{
$SiteID = $spSite.'Site Id'
Write-Output "Site Id:" $SiteID
$spSiteDetails = Invoke-RestMethod -Headers $global:accessToken -Uri "https://graph.microsoft.com/v1.0/sites/$SiteID"
if ($spSiteDetails -ne $null)
{
Write-output "Site Name:" $spSiteDetails.displayName
$Global:SPDetails += [PSCustomObject]@{
SiteId = $SiteID
SiteName = $spSiteDetails.displayName
CreatedDate = ([DateTime]$spSiteDetails.createdDateTime).ToUniversalTime()
}
}
}
WritetoSharePoint
}
catch
{
Write-Output $Error[0]
Write-Output "Error generating SharePoint Details report"
$Error =$null
}
}
# Authenticate to SharePoint
function ConnectToSharePoint
{
try{
$spCredentials = Get-AutomationPSCredential -Name "SharePoint"
Connect-PnPOnline -Url "$spSiteUrl/sites/$spSiteName" -Credential $spCredentials
}
catch{Write-Host "Unable to connect to SharePoint Online.."}
}
function ExecuteReportAPI
{
param([String[]] $Param )
$CSVFileName = $Param[0]
$graphApiUri = $Param[1]
#Write-Output $global:accessToken
$filePath = $env:Temp
try{
Write-Output "Fetching report output by executing Graph API"
$Reports = (Invoke-RestMethod -Method Get -Uri $graphApiUri -Headers $global:accessToken) -Replace "", "" | ConvertFrom-Csv
$Reports | Export-Csv $filePath\$CSVFileName -NoTypeInformation
$Values = @{"Title" = 'SharePoint Reports'}
# Add the file to the Reports folder
Write-Output " Upload file to SharePoint"
$RelativeURL =$spLibraryName
$FolderObject = Get-PnPFolder -Url $RelativeURL
$Upload = Add-PnPFile -Path $filePath\$CSVFileName -Folder $FolderObject
If ($Upload -ne $null)
{
Write-Output "Report sucessfully uploaded"
}
}
catch
{
Write-Error $Error[0]
Write-Output "Exception while fetching SharePoint usage report"
}
}
# Check if the file exists
function WritetoSharePoint{
$csvFileName = Get-AutomationVariable -Name 'SharePointReport'
write-output $csvFileName
# Export to CSV locally
$tempFilePath = "$env:TEMP\1.$csvFileName"
$Global:SPDetails | Export-Csv -Path $tempFilePath -NoTypeInformation
# Upload to SharePoint
$FolderObject = Get-PnPFolder -Url "$spSiteUrl/sites/$spSiteName/$spLibraryName"
Add-PnPFile -Path $tempFilePath -Folder $FolderObject
# Clean up local file
Remove-Item -Path $tempFilePath -Force
}
function main{
ConnecttoGraph
ConnectToSharePoint
GetSharePointDetails
# Microsoft Teams Team Activity Report
ExecuteReportAPI("2.SharePointUsageData.csv","https://graph.microsoft.com/v1.0/reports/getSharePointSiteUsageDetail(period='D30')")
ExecuteReportAPI("3.SharePointUserActivityDetails.csv","https://graph.microsoft.com/v1.0/reports/getSharePointActivityUserDetail(period='D30')")
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment