Extract PSTN and SMS usage from Teams using powershell or Graph

Brass Contributor

I am looking for PSTN and SMS usage teams report using PowerShell and export it into SharePoint list. Any advise ?

 

Thanks

2 Replies

@Audi86 

You can use Microsoft Graph APIs to get the PSTN and SMS usage data for Teams. Here is an example PowerShell script to retrieve the PSTN and SMS usage data for a given time period and export it to a SharePoint list.

  1. First, you need to authenticate to Microsoft Graph using PowerShell. You can use the following code to authenticate and create a session:

 

 

Connect-PnPOnline -Url "https://yourtenant.sharepoint.com/sites/yourSite" -Interactive
Connect-MicrosoftTeams

 

 

  1. Next, you can use the Invoke-RestMethod the cmdlet to call the Microsoft Graph API to get the PSTN and SMS usage data. Here's an example of how to get the data for the last 7 days:

 

 

$startDate = (Get-Date).AddDays(-7).ToString("yyyy-MM-dd")
$endDate = (Get-Date).ToString("yyyy-MM-dd")
$usageData = Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/beta/communications/callRecords?$filter=callType eq 'peerToPeer' and startDateTime ge $startDate and startDateTime le $endDate" -Headers @{Authorization = "Bearer $((Get-MicrosoftAccessToken).AccessToken)"}

 

 

  1. You can then loop through the $usageData variable to extract the data you need, such as the phone number, call duration, and cost.

  2. Finally, you can export the data to a SharePoint list using the Add-PnPListItem cmdlet. Here's an example:

 

 

$web = Get-PnPWeb
$list = Get-PnPList -Identity "Usage Data"
foreach ($data in $usageData) {
    Add-PnPListItem -List $list -Values @{"Phone Number" = $data.resource.@from.phoneNumber; "Call Duration" = $data.resource.duration; "Cost" = $data.resource.cost.amount}
}

 

 

 

Note: The above code is just an example and may need to be modified to fit your specific requirements.

Source: (external link removed by moderator)

@davidlime160Thank you, let me test it out.

i see you have used a Url involving Microsoft Graph. Do i need any permissions beyond Teams admin to use this url. i have been using pnp but never used graph so just wondering?