Office 365 PowerShell License Report

Scenario:

Office 365 PowerShell License Report.

When you are in an environment that changes to licenses are happening every day or a lot of users are activated and deactivated everyday it is very difficult to keep track of the licenses and when it is needed to order new licenses for you subscription.

Under azure portal you are able to see how many license left but again is not a convenient way. I have came up with the below script to get a report everyday for the licenses that the company has and have a report at any time with the number of licenses assigned to the users and how many are free. In general the script gathers the information from MS Online and create an HTML email report to be sent. The script is configured to run on Azure Automation on a schedule basis and manually.

Azure Automation

This next part is provided by Microsoft:

Azure Automation delivers a cloud-based automation and configuration service that provides consistent management across your Azure and non-Azure environments. It consists of process automation, update management, and configuration features. Azure Automation provides complete control during deployment, operations, and decommissioning of workloads and resources.

Let’s check the script in more details

First the script will save in two variables the credentials needed to perform the operation. The first one is the administrator credentials that will be used to gather he information from Azure. The second credentials are the ones that will be used to send the HTML email report to the intended users.

After the script has retrieved the credentials it will ensure that the session is clear and is not conflicting with any previous failed attempt of the script that may has stuck. The script check for any active sessions and if there is any it removes the sessions found. Next, The script will import MSOnline module that is needed to connect to the services, import the commands and gather the information.

When everything has been setup, the script will first check if there are any users without license. Users without license have a grace period of 30 days and after that they will stop to work. So to avoid such situation we need to find out if there are any users without Enterprise E3 or Exchage Online Plan 2 license. If there are any, their UserPrincipalName will be saved in a variable and include it in the email report.

The next part of the script, is another check that it may affect you. The script will check if a user has duplicate licenses. What I mean by this? In Enterprise E3 license, it include Exchange Online Plan 2. So if the company has also Exchange Online Plan 2 License separated, then you might come across this issue. So after the script actually checks if a users has been assigned with Enterpise E3 license and Exchange Online Plan 2 license.

Checks for unlicensed and duplicate licensed users completed

When the checks above will be completed, the script will continue to gather the information that we need. It gathers the total number of each license that is under the subscription, the number of the used licenses and the number of free licenses. The above procedure is done for each license and we save the information in a variable for each license, so the script will provide it on the HTML email report at the end.

Reporting

All the information that has been gathered, is imported in the HTML email report and sent to the intended users. If there is an error during the process, or exception, the script will sent and error email report to inform the intended recipient that something went wrong during the process and what was the error so it will act accordingly.

You can download the script here or copy it from below.

Hope you like it. If you have any questions or anything else please let me know in the comments below.

Related Links:

Solution / Script: