Receive the available storage within your SharePoint Online tenantReceive the available storage within your SharePoint Online tenant

Within the SharePoint admin centre there is that little detailed overview, telling you the available storage out of your total storage. But how do you get that without visiting the SharePoint Admin Centre every day?

Storage Stats in SharePoint Admin Centre
Storage Stats in SharePoint Admin Centre

Retrieve the available storage using PowerShell

The PowerShell script to get the storage is actually really simple:

$AdminCenterURL = "https://tenant-admin.sharepoint.com"
Connect-SPOService -Url $AdminCenterURL

Get-SPOGeoStorageQuota

This will return the following data:

GeoLocation : EU
GeoUsedStorageMB : 1886079
GeoAvailableStorageMB : 176769
GeoAllocatedStorageMB : 0
TenantStorageMB : 2062848
QuotaType : Allocated

So how do we get this in an email on a regular basis?

Create an Azure Runbook

I’m going to use an Azure runbook to run my PowerShell. First step is to create an Automation Account.

Automation Accounts overview
Automation Accounts overview

So I click on the Create option and then complete the following form:

Create Azure Automation Account
Create Azure Automation Account

After a while my Automation Account will be ready.

Created Azure Automation Account successfully
Created Azure Automation Account successfully

In the left hand navigation there is an option for the Runbook to be created:

Created Azure Automation Account
Created Azure Automation Account

I’m going to ignore the example runbooks and I’m creating a new runbook as shown below

create a runbook
create a runbook

Now we can copy in our PowerShell script but if we run the script we will get errors as we haven’t installed the SharePoint Online modules yet.

CommandNotFoundException
CommandNotFoundException

In your Automation Account you can install the required module.

In our case we need Microsoft.Online.SharePoint.PowerShell

Installing Modules in Automation Accounts

Within the left hand navigation the Modules options gives us all we need.

Modules in Automation Accounts
Modules in Automation Accounts

This is all quite easy as you can select the module that we need from the Gallery.

Importing PowerShell Module Mcirosoft.Online.SharePoint.PowerShell
Importing PowerShell Module Mcirosoft.Online.SharePoint.PowerShell

Please note that it can take a while for the module to be imported.

Module import taking a while

Once the module is available we can run our script. But we will still need to sort something out. Without the runbook there is no interaction possible. So we will need to sort out the authentication within the script.

You could of course always install the modules as part of your script by adding the following lines:

Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser -Force
Import-Module Microsoft.Online.SharePoint.PowerShell

$AdminCenterURL = "https://pieterveenstraMVP-admin.sharepoint.com"
Connect-SPOService -Url $AdminCenterURL

Get-SPOGeoStorageQuota

Now we have just one issue left:

no valid OAuth
no valid OAuth

As there is no interaction possible within the Runbook scripts, we will need to connect in a different way.

Credentials stored in the Azure Automation Account

You can store credentials in the Azure Automation Account. And then all we need to do is adjust the Connection-SPOService call as shown below:

Install-Module Microsoft.Online.SharePoint.PowerShell -Scope CurrentUser -Force
Import-Module Microsoft.Online.SharePoint.PowerShell

$AdminCenterURL = "https://pieterveenstraMVP-admin.sharepoint.com"

$creds=Get-AutomationPSCredential -Name 'MyCredentials'

Connect-SPOService -Url $AdminCenterURL -Credential $creds

Get-SPOGeoStorageQuota

Ok, I don’t really like that I can’t use MFA on the account that I specified within the Azure Automation Account, but that is the limitation that we have here.

We will now see the following result returned by the Runbook.

Azure Automation job ran successfully
Azure Automation job ran successfully

Email the result

Ok, there will be a number of options available, but I’m going to use Power Automate to send out the email.

Flow running Azure Automation job
Flow running Azure Automation job

Then we get the output from the job that we created, before we send an email.

A flow to email the Azure Automation output
A flow to email the Azure Automation output

And now we get an email with the following output. We can of course format this a bit better. But I didn’t want to make the Azure Automation script any more complicated as part of this post.

Output with geo location usage shown in email
Output with geo location usage shown in email
Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading