How to organize and set phone numbers in Teams with Power Automate + SharePoint
- Ómar Örn Magnússon
- Sep 12, 2020
- 4 min read
Why?
If you have Phone numbers to manage it can be hard to know where to look for available numbers. Hence the right place is to start to put all your numbers in one central location.
Whether it’s Excel or SharePoint or something else it’s just a good idea to know what numbers you have and who has what number, especially if you are migrating from one system to another, its a good idea to know.
How?
In Iceland there, you need to use Direct Routing for all numbers because Microsoft is not porting numbers in Iceland. If you are like one customer of mine, you usually have numbers in many countries dealing with a few telecom companies via a sip trunk, and to simplify things, you might want to have all your numbers going to the cloud in one way. In some cases, it might even be cheaper, as was the case with one customer.
And the local IT department there wanted a friendly and easy way to manage the phone numbers while migrating from on-premise Skype For Business.
What.
Enter SharePoint + PowerAutomate with some Powershell to accomplish this task; this is how.

You can start using a SharePoint template from Power Automate to create the Flow, or you can build it from scratch. You can also add approvals if that’s needed, but for my use case, it was the It admins who had access to this SharePoint list.
First, I created the SP list, like in the picture above. I added an extra column with a choice of Disabled and Enabled, so it can also bee used to clean up the number from the user.

Then I create and adjust the PowerShell script.
When you use direct routing, you have to use Powershell to enable the enterprise voice features and voicemail within Teams or Skype For Business.
param ([Parameter(Mandatory=$true)]
[string] $email,[Parameter(Mandatory=$true)]
[string] $action,[Parameter(Mandatory=$true)]
[string] $telephoneNumber
)
#split up to get the SamAccountName
$samaccount = $email.Split("@")
$SamAccountName = $samaccount[0]
$UserPrincipalName = $email
$creds = Get-AutomationPSCredential -Name 'Flow Automation' # get credentials from the automation account assets
connect-msolservice -credential $creds # connect to msonline see of the license is ready for use else you cant assign the enterprise voice
#blocked out because customer used licensing groups to accomplish this task
#Set-MsolUserLicense -UserPrincipalName $email -AddLicenses "company:MCOEV" -ErrorAction SilentlyContinue
#Set-MsolUserLicense -UserPrincipalName $email -AddLicenses "company:MCOSTANDARD" -ErrorAction SilentlyContinue
$phonesystemremainiing = $active - $comsumed
if ($action -eq "enable" ){
do
{
$user = Get-MsolUser -UserPrincipalName $email
$haslic = $user.Licenses | where {$_.AccountSkuId -eq "company:MCOEV"}
if (!$haslic) {Write-Output "is not licansed"
Start-Sleep 30
}
}until ($haslic ) # loop until the user has the right licanse
$session = New-CsOnlineSession -Credential $creds # connect to teamsskype online
Import-PSSession $Session -AllowClobber |Out-Null
Get-CsonlineUser $UserPrincipalName |Set-CsUser -EnterpriseVoiceEnabled $true -HostedVoiceMail $true
set-ADUser -Identity $SamAccountName –replace @{'msRTCSIP-Line' ='tel:+'+$telephoneNumber} -OfficePhone $telephoneNumber # set the AD attributes
$teamsonlineuser = get-csonlineuser -Identity $UserPrincipalName | Select-Object LineURI,EnterpriseVoiceEnabled
if ($teamsonlineuser.EnterpriseVoiceEnabled -and $teamsonlineuser.LineURI) {Write-Output "success"}
else {Write-Output "failed"}
}
if ($action -eq "disable" )
{
set-ADUser -Identity $SamAccountName -Clear msRTCSIP-Line, telephoneNumber
}
Now that the PowerShell script and SharePoint list is ready, we can create the Flow
create an automated -from blank Flow

And hook it up to the list created earlier, then we add a control called switch to determine if it’s a disable or enable task.

then we need to look up the user from office365 -> Get user profile so we can determine the right userprincipalname from the username field in sp, (sometimes using the data from the sp gave me the onmicrosoft.com address which caused an error)

If you have the group-based licensing, then you need to check if the user is a member before adding him to the azure ad group.
To see if the output of the “check group membership” has a value, you need to use the action “compose” and count the number of the outputs from the result.


I also like to check how many phone system licenses are left to be sure I have licenses to give the user.
here is the PowerShell script I used for that
$cred = Get-AutomationPSCredential -Name 'Flow Automation'
connect-msolservice -credential $cred
$comsumed =(Get-MsolAccountSku | where {$_.AccountSkuId -eq "company:MCOEV"}).ConsumedUnits
$active =(Get-MsolAccountSku | where {$_.AccountSkuId -eq "company:MCOEV"}).ActiveUnits
$phonesystemremainiing = $active - $comsumed
Write-Output $phonesystemremainiing
that gives me the output that I can then use to order more licensing or send an email to the person that orders the licenses,
I will have another post later explaining that in more detail and how to use the CSP API.
Now our Flow looks like, in this Flow, I am also checking to see if the user is a part of the old license based Azure Ad group and removing him from that one,

in the last part of the script
$teamsonlineuser = get-csonlineuser -Identity $UserPrincipalName | Select-Object LineURI,EnterpriseVoiceEnabled< if ($teamsonlineuser.EnterpriseVoiceEnabled -and $teamsonlineuser.LineURI) {Write-Output "success"} else {Write-Output "failed"}
that gives me an error of success or failed
I can then use that output to let me know with an email if it failed or send the user that requests the change that the change was successfully applied.
Comments