Teams Telephone Number Export

Teams Telephone Number Export

Usually I focus on more high-level articles, but after building some demos recently I wanted to share a snippet of Code I had used to export telephone numbers and active users from Microsoft Teams for off-line analysis.

Within Microsoft Teams there is the ability for Calling Plans, Direct Routing and Operator Connect to co-exist. Direct Routing by design however relies on external number routing and as such the E164 user numbers are not visible in the Phones area of the TAC (Teams Admin Centre). The script below and also available on GitHub will allow you to create a CSV export of active telephone numbers assigned to users or call queues by PSTN type, Operator Connect, Direct Routing or Calling Plan.

Feel free to use on your own platforms to obtain insights if required.

Pre-requisites and method of utilisation:

  1. Teams Module installed in PowerShell.
  2. The Script saved to a file you can run from a PowerShell session
  3. A log in that will allow access to the required commands (Get-CsOnlineUser) across your tenant

Try {
? ? $includeFields = "DisplayName","LineUri","OnlineDialOutPolicy","UsageLocation"
? ? $OCFile = "OperatorConnectNumbers.csv"
? ? $CPFile = "CallingPlanTelephoneNumbers.csv"
? ? $DRFile = "DirectRoutingNumbers.csv"


? ? Function Select-FolderDialog
? ? {
? ? ? ? param([string]$Description="Select Folder",[string]$RootFolder="Desktop")
? ? ? ? [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null? ? ?


? ? ? ? $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
? ? ? ? $objForm.Rootfolder = $RootFolder
? ? ? ? $objForm.Description = $Description
? ? ? ? $Show = $objForm.ShowDialog()


? ? ? ? $loop = $true
? ? ? ? while($loop)
? ? ? ? {
? ? ? ? ? ? If ($Show -eq "OK")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? $loop = $false
? ? ? ? ? ? ? ? Return $objForm.SelectedPath
? ? ? ? ? ? } Else {
? ? ? ? ? ? ? ? return
? ? ? ? ? ? }
? ? ? ? }
? ? }


? ? Function ConnectToTeams
? ? {
? ? ? ? if (Get-Module -ListAvailable -Name MicrosoftTeams){
		? ? try { $null = Get-CsTenant } catch { Connect-MicrosoftTeams }
		? ? Return 1
	? ? } else {
		? ? Return 0
	? ? }
? ? }
? ? Function Get-OC-Numbers($errorcount)
? ? {
? ? ? ? Get-CsOnlineUser | where-object {$_.LineUri -ne $nul } | where-object {$_.FeatureTypes -notcontains "CallingPlan"}? | where-object {$_.OnlineVoiceRoutingPolicy -eq $null} | Select DisplayName, LineUri, OnlineDialOutPolicy, UsageLocation | Export-Csv -path $folder\$OCFile -NoTypeInformation
? ? ? ? if($error.count -ne $errorcount){ Return 0} else {return 1}
? ? }


? ? Function Get-CP-Numbers($errorcount)
? ? {
? ? ? ? Get-CsOnlineUser | where-object {$_.LineUri -ne $nul } | where-object {$_.FeatureTypes -contains "CallingPlan"}? | Select $includeFields | Export-Csv -path $folder\$CPFile -NoTypeInformation
? ? ? ? if($error.count -ne $errorcount){ Return 0} else {return 1}
? ? }


? ? Function Get-DR-Numbers($errorcount)
? ? {
? ? ? ? Get-CsOnlineUser | where-object {$_.LineUri -ne $nul } | where-object {$_.FeatureTypes -notcontains "CallingPlan"}? | where-object {$_.OnlineVoiceRoutingPolicy -ne $null} | Select DisplayName, LineUri, OnlineDialOutPolicy, UsageLocation | Export-Csv -path $folder\$DRFile -NoTypeInformation
? ? ? ? if($error.count -ne $errorcount){ Return 0} else {return 1}
? ? }


? ? Function CreateFiles
? ? {
	if ((gi -ErrorAction Ignore $folder\$OCFile).IsReadOnly) {Return 2}
	if ((gi -ErrorAction Ignore $folder\$CPFile).IsReadOnly) {Return 2}
	if ((gi -ErrorAction Ignore $folder\$DRFile).IsReadOnly) {Return 2}


	Add-Type -AssemblyName PresentationFramework
? ? ? ? $msgBoxInput =? [System.Windows.MessageBox]::Show('Would you like to proceed, existing Telephone Files in '+$folder+' will be deleted?', 'Confirmation', 'YesNoCancel','Error')


? ? ? ? switch? ($msgBoxInput) {
? ? ? ? ? ? 'Yes' {
		$state = Get-OC-Numbers($error.count)
		$state = Get-CP-Numbers($error.count)
		$state = Get-DR-Numbers($error.count)
		Return 1
? ? ? ? ? ? }
? ? ? ? ? ? 'No' {
? ? 	? ? ? ? Exit
? ? ? ? ? ? }
? ? ? ? ? ? 'Cancel' {
? ? 	? ? ? ? Exit
? ? ? ? ? ? }

? ? ? ? }
? ? }


? ? echo "Connecting to Teams. If you are not already connected you will receive a pop up or web browser prompt."
? ? cmd /c pause?
? ? $connected = ConnectToTeams
? ? if ($connected -eq 0)
? ? {
	echo "Microsoft Connection failed, eiter the module was not installed or the connection failed"
	Exit
? ? }


? ? if ($folder = Select-FolderDialog) {
	  $CreationStatus = CreateFiles
	  if($CreationStatus -eq 1) {echo "Files created" }
	  if($CreationStatus -eq 2) {echo "Read Only File found, terminating."}
? ? }
}


catch
{
  $ErrorMessage = $_.Exception.Message
  $FailedItem = $_.Exception.ItemName
  Echo "Failed to create the files: $ErrorMessage"
}        

Once Run you should have 3 CSV files you can open in Excel and peruse at your comfort. If you feel you need more fields included, please change line 2, the $includeFields variable to the fields you require. To Find a full list look up the command Get-CSOnlineUser at learn.microsoft.com.

Three Excel windows showing the exported CSV files
Arjen Beltman

Senior Manager, IT Operations North Europe & Iberia at Bacardi-Martini B.V.

1 年

Many thanks for the script, Tim! I runs fine but I was wondering if there is a possibility to also include the Telephone numbers that have been unassigned. This will allow me to review numbers I can potentially cancel from our OC provider.

回复

要查看或添加评论,请登录

Tim Russell的更多文章

  • How does the removal of ADP impact my business?

    How does the removal of ADP impact my business?

    You may have seen the news, and wondered how ADP being turned off will impact your business devices. Apple ADP, or…

  • The Importance of a Clear and Complete Mission: Lessons from Skydiving

    The Importance of a Clear and Complete Mission: Lessons from Skydiving

    I like to use real world examples when sharing best practices and tips, today I am sharing Skydiving as my example, one…

    1 条评论
  • Cisco Norway EBC Round Up

    Cisco Norway EBC Round Up

    Oslo airport, Thursday afternoon, and I’m sitting here excited to write about the last 24 hours. I had the immense…

    4 条评论
  • CommsVerse 2023

    CommsVerse 2023

    This year I managed to get to the first day of CommsVerse 2023, a yearly event ran for the Microsoft Teams community…

    11 条评论
  • The Three Laws

    The Three Laws

    Have you ever read the book 'I, Robot' by Isaac Asimov? If not, it may be worth reading his collection of short stories…

    2 条评论
  • Teams in one Page

    Teams in one Page

    Sometime ago I was given a hand drawn diagram of Teams. It was simple yet concise and I decided to digitise it.

    15 条评论
  • Trust

    Trust

    For the last 16 years I have either been a home based worker, or have spent a fair proportion of my time working from…

    11 条评论

社区洞察

其他会员也浏览了