@@ -14,67 +14,36 @@ Function Invoke-ListExternalTenantInfo {
1414 $Headers = $Request.Headers
1515 Write-LogMessage - headers $Headers - API $APIName - message ' Accessed this API' - Sev ' Debug'
1616
17-
18-
19- # Interact with query parameters or the body of the request.
20- $Tenant = $Request.Query.tenant
21- $TenantFilter = $Request.Query.tenantFilter
22-
23- # Normalize to tenantid and determine if tenant exists
24- $TenantId = (Invoke-RestMethod - Method GET " https://login.windows.net/$Tenant /.well-known/openid-configuration" ).token_endpoint.Split(' /' )[3 ]
25-
26- if ($TenantId ) {
27- $GraphRequest = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByTenantId(tenantId='$TenantId ')" - NoAuthCheck $true - tenantid $TenantFilter
28- $StatusCode = [HttpStatusCode ]::OK
17+ $HttpResponse = [HttpResponseContext ]@ {
18+ StatusCode = [HttpStatusCode ]::OK
19+ Body = " Default response, you should never see this"
2920 }
3021
31- if ($GraphRequest ) {
32-
33- $TenantDefaultDomain = $GraphRequest.defaultDomainName
34-
35- $body = @"
36- <?xml version="1.0" encoding="utf-8"?>
37- <soap:Envelope xmlns:exm="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:ext="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
38- <soap:Header>
39- <a:Action soap:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>
40- <a:To soap:mustUnderstand="1">https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc</a:To>
41- <a:ReplyTo>
42- <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
43- </a:ReplyTo>
44- </soap:Header>
45- <soap:Body>
46- <GetFederationInformationRequestMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
47- <Request>
48- <Domain>$TenantDefaultDomain </Domain>
49- </Request>
50- </GetFederationInformationRequestMessage>
51- </soap:Body>
52- </soap:Envelope>
53- "@
54-
55- # Create the headers
56- $AutoDiscoverHeaders = @ {
57- ' Content-Type' = ' text/xml; charset=utf-8'
58- ' SOAPAction' = ' "http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation"'
59- ' User-Agent' = ' AutodiscoverClient'
22+ try {
23+ if ($Request.Query.tenant ) {
24+ $Tenant = $Request.Query.tenant
25+
26+ # Normalize to tenantid and determine if tenant exists
27+ $TenantId = (Invoke-RestMethod - Method GET " https://login.windows.net/$Tenant /.well-known/openid-configuration" ).token_endpoint.Split(' /' )[3 ]
28+
29+ if ($TenantId ) {
30+ $GraphRequest = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/tenantRelationships/findTenantInformationByTenantId(tenantId='$TenantId ')" - NoAuthCheck $true - tenantid $env: TenantID
31+ $StatusCode = [HttpStatusCode ]::OK
32+ $HttpResponse.Body = [PSCustomObject ]@ {
33+ GraphRequest = $GraphRequest
34+ }
35+ } else {
36+ $HttpResponse.StatusCode = [HttpStatusCode ]::BadRequest
37+ $HttpResponse.Body = " Tenant $ ( $Tenant ) not found"
38+ }
39+ } else {
40+ $HttpResponse.StatusCode = [HttpStatusCode ]::BadRequest
41+ $HttpResponse.Body = " Tenant parameter is required"
6042 }
61-
62- # Invoke
63- $Response = Invoke-RestMethod - UseBasicParsing - Method Post - Uri ' https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc' - Body $body - Headers $AutoDiscoverHeaders
64-
65- # Return
66- $TenantDomains = $Response.Envelope.body.GetFederationInformationResponseMessage.response.Domains.Domain | Sort-Object
43+ } catch {
44+ $HttpResponse.StatusCode = [HttpStatusCode ]::InternalServerError
45+ $HttpResponse.Body = " Something went wrong while trying to get tenant info for tenant $ ( $Tenant ) : $ ( $_.Exception.Message ) "
6746 }
6847
69- $results = [PSCustomObject ]@ {
70- GraphRequest = $GraphRequest
71- Domains = @ ($TenantDomains )
72- }
73-
74- # Associate values to output bindings by calling 'Push-OutputBinding'.
75- Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
76- StatusCode = $StatusCode
77- Body = $results
78- })
79-
48+ Push-OutputBinding - Name Response - Value $HttpResponse
8049}
0 commit comments