Sharepoint storage, most companies probably have more of it than they will ever need. But there are those customers who treat it like a file share and dumping ground. Wouldn’t it be nice if you could send an automated warning if they’re getting close to the quota limit?
Note: this solution uses an undocumented function of the Sharepoint REST API and continued functionality is not guaranteed (this same endpoint is used by the UI).
Pictured: the sharepoint storage quota when nobody is paying attention.
Sharepoint REST API
To retrieve the storage quota all we have to do is hit https://contoso-admin.sharepoint.com/_api/StorageQuotas()?api-version=1.3.2
with a GET.
1
(Invoke-RestMethod -Method "GET" -Headers $SPheader -Uri "https://contoso-admin.sharepoint.com/_api/StorageQuotas()?api-version=1.3.2")
The result:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"@odata.context":"https://contoso-admin.sharepoint.com/_api/$metadata#StorageQuotas",
"value":[
{
"@odata.type":"#Microsoft.Online.SharePoint.MultiGeo.Service.StorageQuota",
"@odata.id":"https://contoso-admin.sharepoint.com/_api/Microsoft.Online.SharePoint.MultiGeo.Service.StorageQuota00000000-0000-0000-0000-000000000000",
"@odata.editLink":"Microsoft.Online.SharePoint.MultiGeo.Service.StorageQuota00000000-0000-0000-0000-000000000000",
"GeoAllocatedStorageMB":0,
"GeoAvailableStorageMB":1286862,
"GeoLocation":"EUR",
"GeoUsedStorageMB":3028786,
"QuotaType":0,
"TenantStorageMB":4315648
}
]
}
The two important properties here are GeoUsedStorageMB
and TenantStorageMB
. The first is the amount of storage that is in use, and the other is the amount of total storage assigned to the tenant.
If we divide one by the other we can arrive at a nice human readable percentage for how full the storage is.
1
$UsedStoragePercentage = [int](($storageQuota.GeoUsedStorageMB / $storageQuota.TenantStorageMB) * 100)
Full Script
With those elements combined we get our full script.
1
2
3
4
# Note that this script assumes that authentication has already been handled and $SPheader contains a valid header object with accesstoken.
$storageQuota = (Invoke-RestMethod -Method "GET" -Headers $SPheader -Uri "https://contoso-admin.sharepoint.com/_api/StorageQuotas()?api-version=1.3.2").value
$UsedStoragePercentage = [int](($storageQuota.GeoUsedStorageMB / $storageQuota.TenantStorageMB) * 100)