Skip to the main content.

ScriptRunner Blog

Read out OneDrive Storage – Act in Time

Table of Contents

 

 

Post Featured Image

Do you work in IT administration and manage OneDrive accounts? There is a lot of work involved in this topic! Especially for expanding and already large companies, it is worth taking a look at how automation and monitoring can help in this area. Besides: Who wants to click through boring routine tasks manually via graphical interfaces?

Far too often, this "daily business" takes up too much time. Don't get me wrong: standard operations should be considered prio 1. And that's exactly why it is important to make your life easier with the help of smart scripts. Many administrative tasks can be automated. You will decrease the number of errors (which are, after all, human in manual tasks), and also make better use of your time and even possibly free up some time
.

 

Using PowerShell to Retrieve Storage Usage  

One task that falls under this routine work is the administration of M365. As more and more companies have jumped on the OneDrive bandwagon in recent years and the intensity of usage continues to rise, the limited storage capacity pushes some users to their limits. If central monitoring is now lacking here, you run the risk of falling into a mode of pure "reacting to problems", having to put out fires and rarely being able to proactively address the challenge.

Tickets with the titles "OneDrive no longer works" or "Drive broken" will pop up more frequently, whereupon you view all of your employees' OneDrive drives via the admin interface and realize that they are merely full. An unnecessary task that can be prevented with monitoring.

With just a few lines of code, you have the opportunity to pre-emptively intervene in the process here - be notified as soon as an account fills up, when storage usage suddenly spikes, or measure how storage usage changes over the months and years. The blind spot (as far as online storage usage is concerned) disappears!

 

What exactly do I want to show here? 

A smart way to use PowerShell to check and therewith manage a user's storage.

What are the Requirements? 

You only need the SharePoint module for PowerShell and access to the SharePoint Admin Portal. I'll show you how to install said module in a moment. Through the portal, you will also find out your admin URL, if not already known. This corresponds to the domain of the admin center and is needed for the login process from PowerShell. In my case this is: "learningitdotio-admin.sharepoint.com".


1_SharePoint Admin Center

We can log in using this URL:


# ModuleCheck: Installs PSTeams if not available
if(-not (Get-Module -ListAvailable Microsoft.Online.SharePoint.PowerShell)) {
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force
}
else {
Write-Verbose "Microsoft.Online.SharePoint.PowerShell is already installed."
}


# Defining Admin URL
$adminurl = "https://learningitdotio-admin.sharepoint.com"

# Connect to SharePoint-Site
Connect-SPOService -Url $adminURL

Now that we are successfully connected to SharePoint, the question is how to get the users' OneDrive information. By changing the URL, we can get the corresponding information. To do this, just include the user in the URL (in the correct format), for which you need the mail address. Append the string "/personal/" to the known admin URL and add the mail address at the end, replacing all punctuation marks used with an underscore. So "tterraform@learningitdotio.onmicrosoft.com" will result in the following URL:

[https://learningitdotio-my.sharepoint.com/personal/tterraform_learningitdotio_onmicrosoft_com]

Provided that all necessary mails or UPNs are now available, you can create this URL within a loop and then access the URLs. For the access, we use the cmdlet "Get-SPOSite" and specify the URL as "Identity". Using a pipe select, we can filter the data in advance. The query then looks like this:


$user = 'https://learningitdotio-my.sharepoint.com/personal/tterraform_learningitdotio_onmicrosoft_com'

$information = Get-SPOSite -Identity $user |
Select-Object Owner, StorageUsageCurrent, StorageQuota, Status

If the authentication was successful in the step before, you should get an output like this when querying the $information variable:

2_information-Variable output

We get information about the owner, the status of the site, and know how big it is or how much of its capacity is occupied as we want to know if anyone is getting close to their storage limit.

Since we now want to know what percentage of the space used or free, and as the data should be in GB, we add various arithmetic calculations:


# ModuleCheck: Installs PSTeams if not available
if(-not (Get-Module -ListAvailable Microsoft.Online.SharePoint.PowerShell)) {
Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force
}
else {
Write-Verbose "Microsoft.Online.SharePoint.PowerShell is already installed."
}



# Defining Admin URL
$adminurl = "https://learningitdotio-admin.sharepoint.com"

# Connect to SharePoint-Site
Connect-SPOService -Url $adminURL
$user = 'https://learningitdotio-my.sharepoint.com/personal/tterraform_learningitdotio_onmicrosoft_com'

# Get Information
$information = Get-SPOSite -Identity $user | Select-Object Owner, StorageUsageCurrent, StorageQuota, Status

# Calculating
$UsedGB = $information.StorageUsageCurrent / 1024
$TotalGB = $information.StorageQuota / 1024
$UsedPer = $UsedGB / $TotalGB * 100

 

Conclusion

You now have a template that you can use to monitor your OneDrive and SharePoint environment. In this way, you will notice in time that the storage is filling up - even before the limit is exceeded, before failures occur, and before the respective users start writing tickets.

Another step to consider is linking this query to teams webhooks. With webhooks, you can use PowerShell to have Teams messages sent to you as soon as an event (such as OneDrive storage filling up) occurs.

Read the series of articles on webhooks here:

 

 

Good2know

How to Easily Configure Your Targets 

With the current ScriptRunner version, PortalEdition R4, it is possible to set up a SharePoint target directly. To do this, select the M365 target under the menu item 'Targets'.

good2know-scriptrunner-target-m365

The "Activate Service" button can be used to create an M365 account for SharePoint online, among other things.

good2know-scriptrunner-activate-service

In the last window, the SharePoint URL and the appropriate credentials can now be defined.

good2know-scriptrunner-credentials

This is the easiest way to create a SharePoint target in ScriptRunner.

If you prefer to target SharePoint Online from within your script, there is a suitable library in our ActionPacks as an alternative. 

Sprechen Sie lieber SharePoint Online aus Ihrem Skript heraus an, so gibt es als Alternative in unseren AktionPacks eine passende .

Have fun with the setup and #KeepOnScripting.

 

Get to Know ScriptRunner in a Demo

 

 

 

Related Links

Related posts

3 min read

ScriptRunner now available in the Microsoft Azure Marketplace

6 min read

Managing Microsoft Exchange with PowerShell

2 min read

VMUG Webcast: Mastering VMware Management with PowerCLI

About the author: