How do you automate splitting up unassigned number ranges in Skype for Business?

TeamsvS4B

The scenario is that a customer is moving several different AD environments to a new Office 365 tenant as a step towards consolidating all resources in one place. This will take place over a long period of time. In one of the environments, they have a Skype for Business Server with enterprise voice activated for many users. A subset/category of these users is being moved over to the new tenant. New users of other subsets/categories will still be created in the old environment, and therefore they rely on unassigned number ranges in Skype for Business to assign numbers to new Skype for Business users automatically.

They have an SBC connected to Teams Direct Routing. The mediation server of the Skype for Business environment is connected to the SBC.  All the numbers for the users that are being moved are not in a set range, its very fragmented. The complicated part is then for each number you move, you need to split the number range the user’s number is part of in two. This so the number is moved out of the range. When you have over 300 numbers you don’t want to do this manually! After being able to wrap my head around the problem I managed to write a script that automates this whole process. It also adds the moved number to a voice route on the mediation server so all incoming calls to that number are pointed towards the SBC and Direct Routing. The script also activates enterprise voice and assigns the number to a Teams user in the new tenant.

 

$Users = Import-Csv -Path "c:\temp\users.csv"
foreach($user in $Users){
#Gets all unassigned number ranges. This needs to be done for each user in the foreach loop, as you keep changing the ranges.
$UnasignedNumbers = Get-CsUnassignedNumber
#Need to loop through all ranges to find which one the users number belong to.
foreach($Range in $UnasignedNumbers){
#From Get-CsUnassignedNumber you only get the first and last number of a range, not a number range readable in PowerShell.
#We need to take away "tel:+47" from both start and end of the range.
$StartRange = [int64]($Range.NumberRangeStart).Replace("tel:+47","")
$EndRange = [int64]($Range.NumberRangeEnd).Replace("tel:+47","")
#Now we create a number range readable in PowerShell. $NumberRange contains all the numbers in the range, nto jsut start and end.
$NumberRange = $StartRange..$EndRange
#We need to take away "tel:+47" from the users number.
$StrippedNumber = ($User.LineURI).Replace("tel:+47","")
#If the users number is inside the currently processed number range we will go on at split the range its in.
If($NumberRange -contains $StrippedNumber){
#The start of the range above the users number.
$OneUp = "tel:+47"+([int64]$StrippedNumber+1)
#The end of the range below the users number.
$OneDown = "tel:+47"+([int64]$StrippedNumber-1)
#Defiens the names of the two ranges. Use millisecon and a pause so you dont accedently give two ranges the same name.
$LowRangeName = "Unasigned Numbers"+(Get-Date)+(Get-Date).Millisecond
Start-Sleep -Seconds 1
$HighRangeName = "Unasigned Numbers"+(Get-Date)+(Get-Date).Millisecond
#Removes the number range the users number is a part of.
Remove-CsUnassignedNumber -Identity $range.Identity
#If the users number is the first one in the old range there is no need for creating a new range below the users number.
#If its not the first number in the old range, you go on and create a new range below the users number.
If($StrippedNumber -eq $StartRange){"Low range not needed!"}
Else{New-CsUnassignedNumber -Identity "$LowRangeName" -AnnouncementName "Unassigned Number" -NumberRangeStart $Range.NumberRangeStart -NumberRangeEnd $OneDown}
#If the users number is the last one in the old range there is no need for creating a range above the users number.
#If its not the last number in the old range, you go on and create a new range above the users number.
If($StrippedNumber -eq $EndRange){"High range not needed!"}
Else{New-CsUnassignedNumber -Identity "$HighRangeName" -AnnouncementName "Unassigned Number" -NumberRangeStart $OneUp -NumberRangeEnd $Range.NumberRangeEnd}
}
Else{"Not in range!"}
}
#Removes the number and disables enterprise voice for the user.
Set-CsUser -Identity $user.UPNOld -EnterpriseVoiceEnabled $false -LineURI ""
#Adds the users phonenumber to the voice route that points incoming calls towards the SBC.
$UserNumberForPattern = ($user.LineURI).Replace("tel:","")
$OldNumberPattern = ((Get-CsVoiceRoute -Identity "Route to SBC").NumberPattern).trimend(')')
$NewNumberPattaern = $OldNumberPattern+")|(\"+$UserNumberForPattern+"))"
Set-CsVoiceRoute -Identity "Route to SBC" -NumberPattern $NewNumberPattaern
}
#This part needs to be run with Skype Online PowerShell.
foreach($user in $users){
#Adds the users number to a teams user, activates enterprice voice and sets various policies.
Set-CsUser -Identity $user.UPNNew -EnterpriseVoiceEnabled $true -HostedVoiceMail $false -OnPremLineURI $user.LineURI
Grant-CsOnlineVoiceRoutingPolicy -Identity $user.UPNNew -PolicyName Norway
Grant-CsTeamsUpgradePolicy -Identity $user.UPNNew -PolicyName UpgradeToTeams
Grant-CsTeamsCallingPolicy -Identity $user.UPNNew -PolicyName AllowCalling
}

https://gist.github.com/AlexanderHolmeset/cdc237c6ad59975fd09bae9a6c0d4194

 

 

 

Leave a comment