SharePoint Online: Monitor Site Collection Storage Usage with PowerShell

Requirement: Monitor SharePoint Online storage metrics and Send E-mail on current usage.

SharePoint Online: Storage Usage Report using PowerShell
By default, SharePoint Online storage management is set to "Automatic" and all storage allocations are automatically managed from the central pool of storage. However, In your environment if the storage management option is set to "manual" to fine tune the storage space allocated to each individual site, Its a good idea to set up an alert email, so that you are  notified when sites are nearing the storage limit to make sure they aren't affecting site performance.

So, Let's generate storage metrics report and Send Email to SharePoint Administrators using PowerShell.
#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$AdminUserName = "[email protected]"
$AdminPassword = "Password Goes here"
$EmailFrom = "[email protected]"

#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
  
#Connect to SharePoint Online
Connect-SPOService -url $AdminCenterURL -Credential $Credential

#Get All Site Collections Site Storage Usage
[string]$SiteStorage = Get-SPOSite -Limit All | Select Title, URL, StorageQuota, StorageUsageCurrent, @{Label="Percentage";Expression={[math]::Round( ($_.StorageUsageCurrent / $_.StorageQuota * 100),2)}} | ConvertTo-Html

#Send Email
Send-MailMessage -From $EmailFrom -To $AdminUserName -Subject "Storage Report" -Body $SiteStorage -BodyAsHtml -smtpserver "smtp.office365.com" -usessl -Credential $Credential -Port 587

PnP PowerShell to Monitor Site Storage and Send Email
The built-in storage quota warning emails are typically sent on weekly basis when sites reached the specific warning level. However, those Emails could be too late and sites could reach the maximum storage limit and be set to read-only before the site admin receives the alert email. So, Here is the PnP PowerShell script to monitors storage usage and send alert emails.
#Set Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$EmailTo = "[email protected]"
$AdminUserName = "[email protected]"
$AdminPassword = "Password goes here"

#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
 
#Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential

$CSSStyle = "<style>
table {font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;}
table td, th {border: 1px solid #ddd; padding: 8px;}
table tr:nth-child(even){background-color: #f2f2f2;}
table tr:hover {background-color: #ddd;}
table th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white;}
</style>"

#Get the Site collection Storage details
[string]$EmailBody = Get-PnPTenantSite | Select @{Label="Title";Expression={$_.Title}}, 
                                        @{Label="URL";Expression={$_.URL}}, 
                                        @{Label="Allocated";Expression={$_.StorageMaximumLevel}}, 
                                        @{Label="Used";Expression={$_.StorageUsage}}, 
                                        @{Label="Percentage";Expression={[math]::Round( ($_.StorageUsage/$_.StorageMaximumLevel*100),2)}} | 
ConvertTo-Html -Title "Crescent Inc. Storage Report" -Head $CSSStyle -PreContent "Sites Storage Report"

#Send Email
Send-PnPMail -To $EmailTo -Subject "Storage Report" -Body $EmailBody
And here is the script in action:
sharepoint online site storage report

Please note, although I've used plain passwords in the script, the recommended approach is using App ID - App Secret to authenticate in SharePoint Online! For SharePoint Online Management Shell, You can use an encrypted password file! Once the script is ready, you can schedule it to run once per day with Windows task scheduler or Azure Automation.

Get Alert Emails when Sites Exceed a Threshold Value
What if you want to get notified only when sites are exceeding a particular threshold value in the storage consumption? Say, you want to get alert emails when sites reaches their 90% of allocated storage?
#Set Variables
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$EmailTo = "[email protected]"
$AdminUserName = "[email protected]"
$AdminPassword = "Password goes here"
$PercentageThreshold = 90

#Prepare the Credentials
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUserName, $SecurePassword
 
#Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential

$CSSStyle = "<style>
table {font-family: Arial, Helvetica, sans-serif; border-collapse: collapse; width: 100%;}
table td, th {border: 1px solid #ddd; padding: 8px;}
table tr:nth-child(even){background-color: #f2f2f2;}
table tr:hover {background-color: #ddd;}
table th { padding-top: 12px; padding-bottom: 12px; text-align: left; background-color: #4CAF50; color: white;}
</style>"

#Collect Site usage data
$SiteStorageData = @()
ForEach($Site in (Get-PnPTenantSite))
{
    $SiteStorageData += New-Object PSObject -Property ([ordered]@{
        Title  = $Site.Title
        URL = $Site.URL
        Allocated = $Site.StorageMaximumLevel
        Used = $Site.StorageUsage
        Percentage = [math]::Round( ($Site.StorageUsage/$Site.StorageMaximumLevel*100),2)
    })
}
#Filter Sites with usage percentage exceeding given threshold
$SitesExceeding = $SiteStorageData | Where {$_.Percentage -gt $PercentageThreshold}
If($SitesExceeding -ne $Null)
{
    [string]$EmailBody = $SitesExceeding | ConvertTo-Html -Title "Crescent Inc. Storage Report" -Head $CSSStyle -PreContent "Sites Storage Report"

    #Send Email
    Send-PnPMail -To $EmailTo -Subject "Storage Report" -Body $EmailBody
}
Here is my another post on generating site storage report: SharePoint Online: Site Collection Storage Usage Report using PowerShell

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.