Created
September 4, 2018 06:17
-
-
Save tabs-not-spaces/0eb21145b5c4cf88e331eb237c5d71d3 to your computer and use it in GitHub Desktop.
Get-TenantIdFromDomain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-TenantIdFromDomain { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$FQDN | |
) | |
try { | |
$uri = "https://login.microsoftonline.com/$($FQDN)/.well-known/openid-configuration" | |
$rest = Invoke-RestMethod -Method Get -UseBasicParsing -Uri $uri | |
if ($rest.authorization_endpoint) { | |
$result = $(($rest.authorization_endpoint | Select-String '\w{8}-\w{4}-\w{4}-\w{4}-\w{12}').Matches.Value) | |
if ([guid]::Parse($result)) { | |
return $result.ToString() | |
} | |
else { | |
throw "Tenant ID not found." | |
} | |
} | |
else { | |
throw "Tenant ID not found." | |
} | |
} | |
catch { | |
Write-Error $_.Exception.Message | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment