Powershell Script to Fetch Site Level Statistics

Add-Type -Path "Microsoft.SharePoint.Client.dll"

Add-Type -Path "Microsoft.SharePoint.Client.Runtime.dll"

#Function to Get the size of a Folder in SharePoint Online

Function Get-SPOWebSize([Microsoft.SharePoint.Client.Web]$Web)

{

Try

{

#Frame REST API URL

$RequestUrl = $web.Url+'/_api/web/rootfolder?$select=storagemetrics&$expand=storagemetrics'

$AuthenticationCookie = $ctx.Credentials.GetAuthenticationCookie($Web.Url, $true)

$WebSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession

$WebSession.Credentials = $ctx.Credentials

$WebSession.Cookies.SetCookies($Web.Url, $AuthenticationCookie)

$WebSession.Headers.Add("Accept", "application/json;odata=verbose")

#Invoke Rest Method

$webRequest = Invoke-WebRequest -Uri $RequestUrl -Method Get -WebSession $webSession

# Consume the JSON result

$JSONResponse = $webRequest.Content | ConvertFrom-Json

Return ($JSONResponse.d.StorageMetrics.TotalSize -as [double])

}

Catch [System.Exception]

{

Write-Host -f Red "Error:"$_.Exception.Message

}

}

#Function to Get the item count of a subsite in SharePoint Online

Function Get-SPOWebItemCount([Microsoft.SharePoint.Client.Web]$Web)

{

Try

{

$itemCount = 0

$ctx.load($Web.Lists)

$ctx.executequery()

foreach($list in $Web.Lists)

{

$itemCount += $list.ItemCount

}

Return $ItemCount

}

Catch [System.Exception]

{

Write-Host -f Red "Error:"$_.Exception.Message

}

}

Function Get-SPOSubsiteStats()

{

param (

[Microsoft.SharePoint.Client.Web]

$Web,

[ref]

$outsubsiteSizeMB

)

$StorageMetrics= @()

#Get the Root Folder of the Web

$Ctx = $Web.Context

$Ctx.Load($Web.Webs)

$Ctx.ExecuteQuery()

$childrenSize = 0

#Process all subsites

ForEach($Subweb in $web.Webs)

{

$webSize=0

$StorageMetrics+=Get-SPOSubsiteStats $Subweb ([ref]$webSize)

$webSize= $webSize -as [double]

$childrenSize += $webSize

}

$childrenSize = $childrenSize -as [double]

Write-host -f Yellow "Finding the stats of the subsite:" $web.Url

#Call the function to get Subsite size

$SubSiteSize = Get-SPOWebSize -Web $Web

$SubSiteSize = $SubSiteSize -as [double]

$subsiteSizeMB = [Math]::Round($SubSiteSize/1MB, 2)

$SubsiteSizeOnly = $subsiteSizeMB - $childrenSize

Write-host -f Green "Total Size of the subsite (MB): $SubsiteSizeOnly"

$outsubsiteSizeMB.value = $subsiteSizeMB

$NoOfItems = Get-SPOWebItemCount -Web $Web

Write-host -f Green "Total no. of items in the subsite : $NoOfItems `n"

#Add the result to an Array

$StorageData = New-Object PSObject

$StorageData | Add-Member NoteProperty SiteURL($web.Url)

$StorageData | Add-Member NoteProperty Size-MB($SubsiteSizeOnly)

$StorageData | Add-Member NoteProperty ItemCount($NoOfItems)

$StorageMetrics += $StorageData

Return $StorageMetrics

}

$SiteUrl = Read-Host -Prompt "Enter the Site URL"

$CSVPath = Read-Host -Prompt "Enter the CSV file path"

$UserName = Read-Host -Prompt "Enter the username"

$SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString

#Delete the Output Report, if exists

If (Test-Path $CSVPath) { Remove-Item $CSVPath }

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)

$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)

$web=$Context.Web

$Context.Load($web)

$Context.ExecuteQuery()

$temp=0

#Call the function to get subsite storage metrics

$SubsiteStorageMetrics = Get-SPOSubsiteStats $Web ([ref]$temp)

#Export the results to CSV

$SubsiteStorageMetrics | Export-CSV -LiteralPath $CSVPath -NoTypeInformation

×

Loading...