Skip to content

Commit fd2bee7

Browse files
authored
Merge pull request #343 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 35f096b + bf273a8 commit fd2bee7

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Teams-Sharepoint/Invoke-ListTeamsVoice.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ function Invoke-ListTeamsVoice {
1616

1717
# Interact with query parameters or the body of the request.
1818
$TenantFilter = $Request.Query.tenantFilter
19-
$TenantId = (Get-Tenants | Where-Object -Property defaultDomainName -EQ $TenantFilter).customerId
19+
$TenantId = (Get-Tenants -TenantFilter $TenantFilter).customerId
2020
try {
2121
$Users = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$top=999&`$select=id,userPrincipalName,displayName" -tenantid $TenantFilter)
2222
$Skip = 0
2323
$GraphRequest = do {
2424
Write-Host "Getting page $Skip"
25-
$data = (New-TeamsAPIGetRequest -uri "https://api.interfaces.records.teams.microsoft.com/Skype.TelephoneNumberMgmt/Tenants/$($TenantId)/telephone-numbers?skip=$($Skip)&locale=en-US&top=999" -tenantid $TenantFilter).TelephoneNumbers | ForEach-Object {
26-
Write-Host 'Reached the loop'
25+
$Results = New-TeamsAPIGetRequest -uri "https://api.interfaces.records.teams.microsoft.com/Skype.TelephoneNumberMgmt/Tenants/$($TenantId)/telephone-numbers?skip=$($Skip)&locale=en-US&top=999" -tenantid $TenantFilter
26+
#Write-Information ($Results | ConvertTo-Json -Depth 10)
27+
$data = $Results.TelephoneNumbers | ForEach-Object {
2728
$CompleteRequest = $_ | Select-Object *, @{Name = 'AssignedTo'; Expression = { $users | Where-Object -Property id -EQ $_.TargetId } }
2829
if ($CompleteRequest.AcquisitionDate) {
2930
$CompleteRequest.AcquisitionDate = $_.AcquisitionDate -split 'T' | Select-Object -First 1
@@ -33,11 +34,9 @@ function Invoke-ListTeamsVoice {
3334
$CompleteRequest.AssignedTo ? $null : ($CompleteRequest | Add-Member -NotePropertyName 'AssignedTo' -NotePropertyValue 'Unassigned' -Force)
3435
$CompleteRequest
3536
}
36-
Write-Host 'Finished the loop'
3737
$Skip = $Skip + 999
3838
$Data
3939
} while ($data.Count -eq 999)
40-
Write-Host 'Exiting the Do.'
4140
$StatusCode = [HttpStatusCode]::OK
4241
} catch {
4342
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
@@ -48,7 +47,7 @@ function Invoke-ListTeamsVoice {
4847
Write-Host 'Returning the response'
4948
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
5049
StatusCode = $StatusCode
51-
Body = @($GraphRequest)
50+
Body = @($GraphRequest | Where-Object { $_.TelephoneNumber })
5251
})
5352

5453
}

Modules/CIPPCore/Public/GraphHelper/New-TeamsAPIGetRequest.ps1

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,49 @@ function New-TeamsAPIGetRequest($Uri, $tenantID, $Method = 'GET', $Resource = '4
66

77
if ((Get-AuthorisedRequest -Uri $uri -TenantID $tenantid)) {
88
$token = Get-GraphToken -TenantID $tenantID -Scope "$Resource/.default"
9-
109
$NextURL = $Uri
1110
$ReturnedData = do {
11+
$handler = $null
12+
$httpClient = $null
13+
$response = $null
1214
try {
13-
$Data = Invoke-RestMethod -ContentType "$ContentType;charset=UTF-8" -Uri $NextURL -Method $Method -Headers @{
14-
Authorization = $token.Authorization
15+
# Create handler and client with compression disabled
16+
$handler = New-Object System.Net.Http.HttpClientHandler
17+
$handler.AutomaticDecompression = [System.Net.DecompressionMethods]::None
18+
$httpClient = New-Object System.Net.Http.HttpClient($handler)
19+
20+
# Add all required headers
21+
$headers = @{
22+
'Authorization' = $token.Authorization
1523
'x-ms-client-request-id' = [guid]::NewGuid().ToString()
1624
'x-ms-client-session-id' = [guid]::NewGuid().ToString()
17-
'x-ms-correlation-id' = [guid]::NewGuid()
25+
'x-ms-correlation-id' = [guid]::NewGuid().ToString()
1826
'X-Requested-With' = 'XMLHttpRequest'
1927
'x-ms-tnm-applicationid' = '045268c0-445e-4ac1-9157-d58f67b167d9'
2028
'Accept' = 'application/json'
29+
'Accept-Encoding' = 'identity'
30+
'User-Agent' = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
31+
}
32+
33+
foreach ($header in $headers.GetEnumerator()) {
34+
$httpClient.DefaultRequestHeaders.Add($header.Key, $header.Value)
2135
}
36+
37+
$response = $httpClient.GetAsync($NextURL).Result
38+
$contentString = $response.Content.ReadAsStringAsync().Result
39+
40+
# Parse JSON and return data
41+
$Data = $contentString | ConvertFrom-Json
42+
2243
$Data
2344
if ($noPagination) { $nextURL = $null } else { $nextURL = $data.NextLink }
2445
} catch {
2546
throw "Failed to make Teams API Get Request $_"
47+
} finally {
48+
# Proper cleanup in finally block to ensure disposal even on exceptions
49+
if ($response) { $response.Dispose() }
50+
if ($httpClient) { $httpClient.Dispose() }
51+
if ($handler) { $handler.Dispose() }
2652
}
2753
} until ($null -eq $NextURL)
2854
return $ReturnedData

0 commit comments

Comments
 (0)