Post Notifications About Unused Office 365 Licenses to Teams using Azure Runbooks

Post Notifications About Unused Office 365 Licenses to Teams using Azure Runbooks

I have written several articles on using PowerShell to send alerts and notifications to Microsoft Teams, but up until now they were set up using only the task scheduler. As more and more companies move to the cloud I wanted to see how I could do cloud infrastructure alerting as well. In this article I am using an Azure RunBook to connect to my Office 365 tenant, parse my licenses, and return any that need reconciliation. If you get your Office 365 licenses from a CSP or any other kind of reseller, you may get charged for all of your licenses, applied or not. So it’s a good thing to make sure you don’t have any extra ones lying around.

Set Up the Azure Environment

Resource Group, Runbook and Automation Account Creation

I created a script that you can just change the variables for and it will create the following in your Azure tenant:

  • Automation Account
  • Runbook
  • Resource Group
  • Automation Account Credential (Used to connect to Office 365)

In my example, I am creating a new resource group called “rg-automation” that will contain my runbook and automation account. Also, I am making an Automation credential that the runbook will use to connect to Office 365. All of my assets will be in the North Central US region.

Import AzureAD Module to the Runbook

Next, we must import the AzureAD module to the automation account so we can use all the cmdlets available in the module.

In the Azure Portal, go to your newly create Automation Account and select Modules

Click Browse Gallery and search for AzureAD and select Import

Configure Incoming Webhook

To allow PowerShell to send data to your Teams Channel you will need to configure an incoming Webhook.

  1. In your Team, click on the channel you want the messages to be sent to and click the ellipses (three dots) and select Connectors
  2. In the Incoming WebHook, click Add. If you do not see it on the main page you will have to search for it
  3. Give you webhook a good name. This is what users will see in the Teams chat. Upload an image and then press “Create
  4. Copy the URL and save it for later, it will be needed. Click “Done” when you have saved the URL in a safe spot.
  5. Back in the Teams channel you can see that the webhook has been created.

Create the Azure Runbook

In the Azure Portal, go to your newly create Automation Account and select Runbooks, you will see your newly created RunBook you created earlier

Click on your Runbook and then select Edit

Modify the script to best fit your needs

  • The $IgnoreSkus variable will contain any SKUs you don’t want to alert on. Many times a tenant will contain SKUs that they are trialing or are free so it will always report that there are more available than assigned.
  • The $URI will contain the webhook URL we got above
  • The $AutomationAccounrCredName is the credential we created earlier for our Automation Account
  • The $ItemImage is the image that will be in your webhook, in my example I have a light blue license icon
  • The $Sku is a hash table that contains SKUs and friendly names. The script will attempt to convert the SKU in Office 365 to a user friendly name

The script will also not send anything if there are 0 licenses that need reconsiliation.

You can test it by selecting the Test Pane button when you are editing your runbook. In my case I can see it worked and I have my message in Teams

Once your’ve finished, press Publish to publish your runbook. Then go to Schedules and create a schedule for your runbook. In my examply I have it running 1 time a day at 5PM

Things to Remember

The webhook in Teams has a message size limited to 25 KB. If your message exceeds the limit, Teams responds with a HTTP 413

 

Leave a Reply

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

%d bloggers like this: