A License to Bill

Do you have enough Office 365 licenses? Are you sure? Are they expiring? Is billing getting everything approved to have the licenses renewed? These are not always easy questions. Many admins don't go to the portal often and check this. I wanted to have a simple check that I could run against any tenant and quickly get an email with the license information. The portal is great, but it doesn't lend itself to making an email that you can get to the people who need to see it. This works well for that. I like tables a whole lot more than emailing screenshots of a website.

You do need to log into Office 365 first, before this will work. Since I'm using Office 365's SMTP relay, this also works from anywhere. It's great when you're traveling.

All this code comes with the normal disclaimers, of course. Try this in a lab, if at all possible. You’re on your own here – there’s no warranty on this. If you use it, please leave a comment.

 # Office365-QuickLicenseCount.ps1 - grb - 3/24/2017 - Generates an HTML table of the tenant licenses and emails it.

#Some variables. We need the email addresses and credentials for emailing.
#You need to set a directory for the log and htm files.
$TenantName = (Get-MsolCompanyInformation).DisplayName
$Date = Get-Date -Format 'yyyyMMdd-HHmm'

$to = 'admin@contoso.com'
$from = 'admin@contoso.com'
$LogPath = 'C:\temp\Office365-QuickLicenseCount-'+$Date+'.log'
$FilePath = 'C:\temp\Office365-QuickLicenseCount-'+$Date+'.htm'

$subject = ((Get-MsolCompanyInformation).DisplayName)+' - Office 365 Quick License Count'
$smtpCred = Get-Credential $from

#Start logging what we're doing
Start-Transcript -Path $LogPath

# Set up header information for HTML table.
$Header = @"
 <style>
 TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
 TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;font-family: Verdana; font-size: 10pt;background-color: #6495ED;}
 TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;font-family: Verdana; font-size: 10pt;}
 </style>
"@

# Go get the data
$AccountSkus = Get-MsolAccountSku
$NewAccountSkus = @()

#Add a new column for AvailableUnits
Foreach ($AccountSku in $AccountSkus) {
 $NewAccountSku = $AccountSku

 
 $NewAccountSku | Add-Member -type NoteProperty -name AvailableUnits -value ($AccountSku.ActiveUnits + $AccountSku.WarningUnits - $AccountSku.ConsumedUnits)
 $NewAccountSkus += $NewAccountSku
 }

# display the table as text
$NewAccountSkus | Select SkuPartNumber,ActiveUnits,ConsumedUnits,WarningUnits,AvailableUnits,SuspendedUnits | sort -Property 'SkuPartNumber' | ft

# Define the email body
$body = $NewAccountSkus | Select SkuPartNumber,ActiveUnits,ConsumedUnits,WarningUnits,AvailableUnits,SuspendedUnits | sort -Property 'SkuPartNumber' | ConvertTo-Html -Head $Header | Out-String
Out-File -filepath $FilePath -InputObject $body

#Send the email
Send-MailMessage -From $from -To $to -Subject $subject -Body $body -BodyAsHtml -SmtpServer 'smtp.office365.com' -Port '587' -UseSsl -Credential $smtpCred

# Clean up
Get-PSSession | Remove-PSSession
Stop-Transcript