List All Assigned PSTN numbers in Teams Direct Routing

If you want to list all phone numbers that are assigned to Users, Call Queues or AutoAttendants in Microsoft Teams with direct routing you can use this function:

get-AssignedPSTNNumbers -NumberRangeLow 4712340000 -NumberRangeHigh 4712341000
47=Countrycode

Note! Exclude the "+" sign.


function get-AssignedPSTNNumbers {
[CmdletBinding()]
param (

[Parameter(Mandatory=$false,Position=1,HelpMessage="Enter the lowest number in the numberrange 472233445566")]
[long]$NumberRangeLow,

[Parameter(Mandatory=$false,Position=1,HelpMessage="Enter the Higest number in the numberrange, 473344556677")]
[long]$NumberRangeHigh


)

# get all teams Users with assigned PSTN number in the specified range


$NumberArray = @()
$OnlineVoiceUsers = get-csonlineuser -Filter {VoicePolicy -EQ "HybridVoice" -and EnterpriseVoiceEnabled -EQ $True}|Select-Object displayname,SipAddress,OnPremLineURI
   
if($OnlineVoiceUsers -ne $null)
{
    foreach($item in $OnlineVoiceUsers)
   
    {                 
if (($item.OnPremLineURI.substring(5)) -ge $NumberRangeLow -and ($item.OnPremLineURI.substring(5)) -le $NumberRangeHigh)
{


        $myObject1 = New-Object System.Object
        $myObject1 | Add-Member -type NoteProperty -name "Displayname" -Value $Item.displayname
        $myObject1 | Add-Member -type NoteProperty -name "SipAddress" -Value $item.SipAddress.substring(4)
        $myObject1 | Add-Member -type NoteProperty -name "PhoneNumber" -Value $Item.OnPremLineURI
        $NumberArray += $myObject1         
       
}

    }
}

$VoiceApplications=get-CsOnlineApplicationInstance | select displayname,userprincipalname,phonenumber

foreach($item in $VoiceApplications)
    {                 
if (($item.PhoneNumber.substring(5)) -ge $NumberRangeLow -and ($item.PhoneNumber.substring(5)) -le $NumberRangeHigh)
{


        $myObject1 = New-Object System.Object
        $myObject1 | Add-Member -type NoteProperty -name "Displayname" -Value $Item.displayname
        $myObject1 | Add-Member -type NoteProperty -name "SipAddress" -Value $item.UserPrincipalName
        $myObject1 | Add-Member -type NoteProperty -name "PhoneNumber" -Value $Item.PhoneNumber
$NumberArray += $myObject1         
}

    }
$NumberArray | Sort-Object PhoneNumber   
   
}

Comments

Popular Posts