Prettify and Sync Teams Phone Numbers to Azure AD

I am a very data and fact driven guy. I strongly believe that clean, consistent and structured data is a tremendously important aspect of…

I am a very data and fact driven guy. I strongly believe that clean, consistent and structured data is a tremendously important aspect of information technology.

An example of inconsistency to which probably most Teams Phone admins can relate is that there is no relation between a Teams user’s LineURI and the BusinessPhones property of the same user in Azure AD.

The problem is that throughout Microsoft 365 only the values in Azure AD are published on profile cards. This includes Teams and Outlook. A different phone number in Azure AD than in Teams can create all kinds of problems. Inbound calls might not get routed to a Teams user or calls may be transferred to a wrong user if a call is placed via the number available on a profile card.

Here’s an example of a profile card which shows the user’s phone number from Azure AD.

If we check the same user in Teams Admin Center, we can see that he has a different LineURI.

When it comes to phone numbers in Teams, I view the LineURI as the single source of truth. Writing a simple script which mirrors the LineURI to Azure AD seemed like an easy task.

But then I realized that I can only get the raw value of the LineURI which doesn’t include any spaces which makes it hard to read or spell on profile cards.

While the phone number is displayed nicely formatted according to the local and regional format in TAC or in the Teams Client, it looks like this in PowerShell.

The Teams Client also displays the number in the correct format.

I started to dig around in the browser dev tools to try and see if I could find these formatted numbers anywhere. Not luck. I could only ever find the raw values. I did some more research and learned that there are a couple of already existing and well-established libraries for various programming languages such as Python or Node.JS.

A few years ago, I made some attempts to learn the basics of Python but since I never really used it in real life, I kind of forgot everything. Thanks to ChatGPT I then learned that it’s actually possible to run python code inline in any PowerShell script. Good for me.

Now I have a neat little script which not only copies the LineURI of all Teams Phone users to Azure AD, but it also prettifies them in the same way as TAC and the Teams client do!

The Script

Note: The script has only been tested on Windows 11 using Python 3.11.1.

Prerequisties

  1. Install Python on your machine
  2. Run the following command to install the phonenumbers library python -m pip install phonenumbers
  3. Clone the repository from my GitHub account 
    git clone [https://github.com](https://github.com)/mozziemozz/TeamsPhoneAutomation.git
  4. Make sure that the MicrosoftTeams and Microsoft.Graph PowerShell modules are installed

Code

This is the script. It connects to Teams and Graph, gets all Teams users with a LineURI and then prettifies each LineURI before it’s set on the user’s BuinessPhones property in Azure AD.

If you prefer connecting via Azure AD Service Principal, there’s also a version in the repo which supports that.

Keep in mind that you need to make sure that your App has all required graph scopes added and consented and that the Service Principal is also assigned the Skype for Business Online Administrator role in Azure AD. For more information on how to set up and automated runbook, please refer to this article.

Result

The output of the script will look like this.

As we can see, the phone number of the Azure AD user object has now been changed to the same number as it is in Teams. For better readability and aesthetics, spaces have been added where users would expect them for a phone number from the respective region.

Unfortunately, it can take multiple days until updated phone numbers are visible in Teams or Outlook due to heavy client-side caching.

I hope that this script helps you to keep LineURIs and BusinessPhones in Azure AD in sync or that you can even use it to automate that process.

Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Hosted on GitHub Pages
Built with Hugo
Theme Stack designed by Jimmy