SharePoint Online: PS Script to configure the Quota Warning value for all the site collections

Steel Contributor

When you are managing a big tenant with many site collection the Quota alert is really useful to follow the consumption of your Tenant quota.

So you can use the quota warning system to be informed (as site collection Admin) when that quota is raised and not wait the User (site owner) complain.

 

Because the management can change during the time, I create that script to apply the configuration for all the site collections (based on a filter set you can adapt to your organization).

 

[string]$username = "adminaccount@tenant.onmicrosoft.com"
[string]$PwdTXTPath = "C:\SECUREDPWD\ExportedPWD-$($username).txt"
[string]$ExportAllUserLogin = ""

[double]$QuotaAlertPercentValue = 0.9 # 90%
[integer]$QuotaStorageAlertToApply = 0

function Load-DLLandAssemblies
{
	[string]$defaultDLLPath = ""

	# Load assemblies to PowerShell session 

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)

	$defaultDLLPath = "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
	[System.Reflection.Assembly]::LoadFile($defaultDLLPath)
}
	
cls
Write-Host " ------------------------------------------------------------ "
write-Host "                                                              "
write-Host " Reset the quota alert with $($QuotaAlertPercentValue)        "
write-Host "                                                              "
Write-Host " -----------------------------------------------------------  "

Load-DLLandAssemblies

$secureStringPwd = ConvertTo-SecureString -string (Get-Content $PwdTXTPath)
$adminCreds = New-Object System.Management.Automation.PSCredential $username, $secureStringPwd

Connect-SPOService -Url https://tenant-admin.sharepoint.com -credential $adminCreds -ErrorAction SilentlyContinue -ErrorVariable Err

#Retrieve all site collection infos
$sitesInfo = Get-SPOSite -Template "STS#0" -Limit ALL | Sort-Object -Property url | Select *
# Other filtering samples
#$sitesInfo = Get-SPOSite  -Filter {Url -like '*tenant.sharepoint.com/sites/ca*' } -Template "STS#0" -Limit ALL | Sort-Object -Property url | Select *
#$sitesInfo = Get-SPOSite -Filter {Url -like '*sgs.sharepoint.com/sites/ca-*' } -Template "STS#0" -Limit ALL | Sort-Object -Property url | Select -first 1 *

[int]$i = 1;
[int]$ItemsChanged = 0;

Write-Host " >>>> Number of sites found:", $sitesinfo.count

foreach ($site in $sitesInfo)
{
	Write-Host " >>>>  Site Num to check:", $i  -foregroundcolor green
	$QuotaStorageAlertToApply = [convert]::ToInt32($site.StorageQuota, 10)
	Write-Host "     ===>> Storage Quota before the percentage:", $QuotaStorageAlertToApply, "- StorageQuotaAlertPercent:", $QuotaAlertPercentValue -foregroundcolor yellow
	$QuotaStorageAlertToApply =  $($QuotaStorageAlertToApply * $QuotaAlertPercentValue)
	Write-Host "     ===>> Storage Quota before round command:", $QuotaStorageAlertToApply -foregroundcolor yellow

	$QuotaStorageAlertToApply = [math]::Round($QuotaStorageAlertToApply)

	Write-Host "   >>> Site details:", $site.Url , "- Current Size:", $site.StorageUsageCurrent, "- Storage Quota:", $site.StorageQuota -foregroundcolor green
	Write-Host "     ===>> Storage Quota Warning Applied:", $site.StorageQuotaWarningLevel -foregroundcolor yellow
	Write-Host "     ===>> Storage Quota Warning Theoric:", $QuotaStorageAlertToApply -foregroundcolor yellow
	if($QuotaStorageAlertToApply -ne $site.StorageQuotaWarningLevel)
	{
		$ItemsChanged++;
		Write-Host " >>>>  Number of sites changed:", $ItemsChanged  -foregroundcolor green
		if ($site.Status -ne "Active")
		{
			do
			{
				$site = Get-SPOSite -Identity $site.Url 
				Write-Host "        ===>> Wait 60 sec for the change application"  -foregroundcolor red
				start-sleep -s 60
			}
			until ($site.Status -eq "Active")
		}
		Set-SPOSite -Identity $site.Url -StorageQuotaWarningLevel $QuotaStorageAlertToApply
		Write-Host "     =======>> Storage Quota Warning Applied:", $QuotaStorageAlertToApply -foregroundcolor yellow
	}
	else
	{
		Write-Host "     =======>> NO NEED TO CHANGE Storage Quota Warning " -foregroundcolor red
	}
	$i++;
}

Write-Host " ------------------------------------------------------------------------------------------"
Write-Host " >>>> Number of sites modified:", $ItemsChanged, "- of Total sites checked:", $i -foregroundcolor Red

Fabrice Romelard

 

1 Reply