1+ function Get-SharePointAdminLink {
2+ <#
3+ . FUNCTIONALITY
4+ Internal
5+ #>
6+ [CmdletBinding ()]
7+ param ($Public )
8+
9+ if ($Public ) {
10+ # Do it through domain discovery, unreliable
11+ try {
12+ # Get tenant information using autodiscover
13+ $body = @"
14+ <?xml version="1.0" encoding="utf-8"?>
15+ <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">
16+ <soap:Header>
17+ <a:Action soap:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation</a:Action>
18+ <a:To soap:mustUnderstand="1">https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc</a:To>
19+ <a:ReplyTo>
20+ <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
21+ </a:ReplyTo>
22+ </soap:Header>
23+ <soap:Body>
24+ <GetFederationInformationRequestMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
25+ <Request>
26+ <Domain>$TenantFilter </Domain>
27+ </Request>
28+ </GetFederationInformationRequestMessage>
29+ </soap:Body>
30+ </soap:Envelope>
31+ "@
32+
33+ # Create the headers
34+ $AutoDiscoverHeaders = @ {
35+ ' Content-Type' = ' text/xml; charset=utf-8'
36+ ' SOAPAction' = ' "http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetFederationInformation"'
37+ ' User-Agent' = ' AutodiscoverClient'
38+ }
39+
40+ # Invoke autodiscover
41+ $Response = Invoke-RestMethod - UseBasicParsing - Method Post - Uri ' https://autodiscover-s.outlook.com/autodiscover/autodiscover.svc' - Body $body - Headers $AutoDiscoverHeaders
42+
43+ # Get the onmicrosoft.com domain from the response
44+ $TenantDomains = $Response.Envelope.body.GetFederationInformationResponseMessage.response.Domains.Domain | Sort-Object
45+ $OnMicrosoftDomains = $TenantDomains | Where-Object { $_ -like " *.onmicrosoft.com" }
46+
47+ if ($OnMicrosoftDomains.Count -eq 0 ) {
48+ throw " Could not find onmicrosoft.com domain through autodiscover"
49+ } elseif ($OnMicrosoftDomains.Count -gt 1 ) {
50+ throw " Multiple onmicrosoft.com domains found through autodiscover. Cannot determine the correct one: $ ( $OnMicrosoftDomains -join ' , ' ) "
51+ } else {
52+ $OnMicrosoftDomain = $OnMicrosoftDomains [0 ]
53+ $tenantName = $OnMicrosoftDomain.Split (' .' )[0 ]
54+ }
55+ } catch {
56+ throw " Failed to get SharePoint admin URL through autodiscover: $ ( $_.Exception.Message ) "
57+ }
58+ } else {
59+ $tenantName = (New-GraphGetRequest - uri ' https://graph.microsoft.com/beta/sites/root' - asApp $true - tenantid $TenantFilter ).id.Split(' .' )[0 ]
60+ }
61+
62+ # Return object with all needed properties
63+ return [PSCustomObject ]@ {
64+ AdminUrl = " https://$tenantName -admin.sharepoint.com"
65+ TenantName = $tenantName
66+ SharePointUrl = " https://$tenantName .sharepoint.com"
67+ }
68+ }
0 commit comments