-
Notifications
You must be signed in to change notification settings - Fork 10
/
Get-TeamsPSTNCallRecords.ps1
218 lines (154 loc) · 6.81 KB
/
Get-TeamsPSTNCallRecords.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<#
.SYNOPSIS
Get-TeamsPSTNCallRecords.ps1 - Retrieve Microsoft Teams PSTN call records for Calling Plan and Direct Routing users
.DESCRIPTION
Author: Lee Ford
This tool allows you retireve PSTN call records for Calling Plan and Direct Routing users and save to a file. You can request how many days far back (from now) you wish to retrieve
.LINK
Blog: https://www.lee-ford.co.uk
Twitter: http://www.twitter.com/lee_ford
LinkedIn: https://www.linkedin.com/in/lee-ford/
.EXAMPLE
.\Get-TeamsPSTNCallRecords.ps1 -SavePath C:\Temp -Days 10 -SaveFormat JSON
Retrieve call records for the last 10 days and save as JSON files
.\Get-TeamsPSTNCallRecords.ps1 -SavePath C:\Temp -Days 50 -SaveFormat CSV
Retrieve call records for the last 50 days and save as CSV files
#>
param (
[Parameter(mandatory = $true)][string]$SavePath,
[Parameter(mandatory = $true)][int]$Days,
[Parameter(mandatory = $true)][ValidateSet("JSON", "CSV")]$SaveFormat
)
# Client (application) ID, tenant (directory) ID and secret
$clientId = "<application id goees here>"
$tenantId = "<directory id goes here>"
$clientSecret = '<secret goes here>'
function Get-Calls {
param (
[Parameter(mandatory = $true)][string]$type
)
$remainingDays = $Days
# Set initial to date of date range to end of today/start of tomorrow
$toDateTime = (Get-Date).AddDays(+1)
while ($remainingDays -gt 0) {
$totalRecords = 0
# If remaining days is < 89 set specifically
if ($remainingDays -lt 89) {
$dayBatchSize = $remainingDays
} else {
$dayBatchSize = 89
}
# New remaining days based on new batch
$remainingDays -= $dayBatchSize
# Set from date to be minus day batch size from now
$fromDateTime = ($toDateTime).AddDays(-$dayBatchSize)
# Set dates to correctly formatted strings for query
$toDateTimeString = $toDateTime | Get-Date -Format "yyyy-MM-dd"
$fromDateTimeString = $fromDateTime | Get-Date -Format "yyyy-MM-dd"
$currentUri = "https://graph.microsoft.com/v1.0/communications/callRecords/$type(fromDateTime=$fromDateTimeString,toDateTime=$toDateTimeString)"
Write-Host " - Checking for call records between $fromDateTimeString and $toDateTimeString..." -NoNewline
$content += while (-not [string]::IsNullOrEmpty($currentUri)) {
$apiCall = Invoke-RestMethod -Method "GET" -Uri $currentUri -ContentType "application/json" -Headers @{Authorization = "Bearer $token" } -ErrorAction Stop
$currentUri = $null
if ($apiCall) {
# Check if any data is left
$currentUri = $apiCall.'@odata.nextLink'
# Count total records so far
$totalRecords += $apiCall.'@odata.count'
$apiCall.value
}
}
# Set the to date to start from the previous from date
$toDateTime = $fromDateTime
if ($totalRecords -gt 0) {
Write-Host " Retrieved $totalRecords call records" -ForegroundColor Green
} else {
Write-Host " No records found" -ForegroundColor Yellow
}
}
return $content
}
# Start
Write-Host "`n----------------------------------------------------------------------------------------------
`n Get-TeamsPSTNCallRecords.ps1 - Lee Ford
`n https://github.com/leeford/Get-TeamsPSTNCallRecords - https://www.lee-ford.co.uk
`n----------------------------------------------------------------------------------------------
`n Disclaimer: This script is provided ‘as-is’ without any warranty or support.
`n Use of this script is at your own risk." -ForegroundColor Yellow
# Check Days is a postive number
if ($Days -lt 0) {
Write-Host "Please specify a valid date range (greater than 0 days)" -ForegroundColor Red
break
}
if ($Days -gt 365) {
Write-Warning "Call records are typically only stored for 365 days"
}
# Check Save Path exists
if (-not (Test-Path -Path $SavePath)) {
Write-Host "$SavePath does not exist, please specify a valid path" -ForegroundColor Red
break
}
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$body = @{
client_id = $clientId
scope = "https://graph.microsoft.com/.default"
client_secret = $clientSecret
grant_type = "client_credentials"
}
# Get OAuth Access Token
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body -UseBasicParsing
# Set Access Token
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token
Write-Host "`r`n- Retrieving PSTN call records for the last $Days days"
# Get Direct Routing calls
Write-Host " - Retrieving Direct Routing call records"
$directRoutingCalls = Get-Calls -type "getDirectRoutingCalls"
# Get Calling Plan calls
Write-Host " - Retrieving Calling Plan call records"
$callingPlanCalls = Get-Calls -type "getPstnCalls"
# Save to file
Write-Host "`r`n- Saving PSTN call records to $SaveFormat files"
if ($SaveFormat -eq "JSON") {
if ($directRoutingCalls) {
try {
Write-Host " - Saving Direct Routing call records in JSON format to $SavePath\DirectRoutingCalls.json..." -NoNewline
$directRoutingCalls | ConvertTo-Json | Out-File -FilePath "$SavePath\DirectRoutingCalls.json"
Write-Host " SUCCESS" -ForegroundColor Green
}
catch {
Write-Host " FAILED" -ForegroundColor Red
}
}
if ($callingPlanCalls) {
try {
Write-Host " - Saving Calling Plan call records in JSON format to $SavePath\CallingPlanCalls.json..." -NoNewline
$callingPlanCalls | ConvertTo-Json | Out-File -FilePath "$SavePath\CallingPlanCalls.json"
Write-Host " SUCCESS" -ForegroundColor Green
}
catch {
Write-Host " FAILED" -ForegroundColor Red
}
}
}
elseif ($SaveFormat -eq "CSV") {
if ($directRoutingCalls) {
try {
Write-Host " - Saving Direct Routing call records in CSV format to $SavePath\DirectRoutingCalls.csv..." -NoNewline
$directRoutingCalls | Export-Csv -Path "$SavePath\DirectRoutingCalls.csv"
Write-Host " SUCCESS" -ForegroundColor Green
}
catch {
Write-Host " FAILED" -ForegroundColor Red
}
}
if ($callingPlanCalls) {
try {
Write-Host " - Saving Calling Plan call records in CSV format to $SavePath\CallingPlanCalls.csv..." -NoNewline
$callingPlanCalls | Export-Csv -Path "$SavePath\CallingPlanCalls.csv"
Write-Host " SUCCESS" -ForegroundColor Green
}
catch {
Write-Host " FAILED" -ForegroundColor Red
}
}
}