helpoffice365powershell

Email alerts for low Office 365 licenses!

Posted

Welcome! Recently, one of our larger clients was looking for an easy way to alert when their Microsoft or Office tenant was running low on licenses. Surprisingly, or maybe not surprisingly, Microsoft doesn’t offer a way to automatically alert when license fall below a certain threshold. This client has recently experience a lot of turn over with employees coming and going quite often. Below, is a quick and glorious PowerShell script that helped us easily accomplish this task. As of now, we have the script set to run daily during the work week via Microsoft Task Scheduler.

$smtpserver = “mail.mysmtpserver.com”
$smtpto = “[email protected]
$smtpfrom = “[email protected]

Connect-MsolService -Credential (Get-StoredCredential -Target msol)

$F3Lic = Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq “reseller-account:DESKLESSPACK”}
$F3 = $F3Lic.ActiveUnits
echo “I have found $F3 F3 licenses …”
$F3LicAvailable = $F3Lic.ActiveUnits – $F3Lic.ConsumedUnits
echo “You have $F3LicAvailable F3 licenses avaliable to consume …”

$E3Lic = Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq “reseller-account:SPE_E3”}
$E3 = $E3Lic.ActiveUnits
echo “I have found $E3 E3 licenses …”
$E3LicAvailable = $E3Lic.ActiveUnits – $E3Lic.ConsumedUnits
echo “You have $E3LicAvailable E3 licenses avaliable to consume …”

if ($F3LicAvailable -lt 5) {
Send-MailMessage -SmtpServer $smtpserver -To $smtpto -From $smtpfrom -Subject “Office 365 F3 Usage Report – LOW!!” -Body “There are $F3LicAvailable Office 365 F3 licenses out of $F3 available to consume …” -BodyAsHtml
}

if ($E3LicAvailable -lt 5) {
Send-MailMessage -SmtpServer $smtpserver -To $smtpto -From $smtpfrom -Subject “Office 365 E3 Usage Report – LOW!!” -Body “There are $E3LicAvailable Office 365 E3 licenses out of $E3 available to consume …” -BodyAsHtml
}

Wow! This might look VERY confusing, but I assure you, it really isn’t! We will break it down into a few sections below – as always, don’t hesitate to get in touch if you need help! The first three lines are quite simple and self explanatory, define what SMTP server you need to use to send out alerts. Line two is who to send the alert to (maybe a group or distribution list?) and third line is just a generic from address.

Next, we need to connect to Microsoft’s Online Service to actually run our commands and pull down information. This line is using a VERY cool plugin for PowerShell to actually pull stored credentials from Windows Credential manager. To that end, we will cover this is another post!

The next two sections of the script are really just setting variables and doing some calculations and outputting information to the user if it is ran in interactive mode. You MAY need to adjust the variable in the line below depending on your tenant OR depending on what license you want to report on. We will cover this is the next blog as well.

$F3Lic = Get-MsolAccountSku | Where-Object {$_.AccountSkuId -eq “reseller-account:DESKLESSPACK”}

Finally, we use the awesome Send-MailMessage command to actually fire off the message to our user with useful information. As of now, it will alert if there is less then (-lt 5) licenses per SKU, adjust as needed! Comment if you need help! Pretty slick, huh?

Leave a Reply

Your email address will not be published. Required fields are marked *