@@ -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