Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderHolmeset-zz/cdc237c6ad59975fd09bae9a6c0d4194 to your computer and use it in GitHub Desktop.
Save AlexanderHolmeset-zz/cdc237c6ad59975fd09bae9a6c0d4194 to your computer and use it in GitHub Desktop.
$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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment