Skip to content

Instantly share code, notes, and snippets.

@tabs-not-spaces
Created September 4, 2018 06:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabs-not-spaces/0eb21145b5c4cf88e331eb237c5d71d3 to your computer and use it in GitHub Desktop.
Save tabs-not-spaces/0eb21145b5c4cf88e331eb237c5d71d3 to your computer and use it in GitHub Desktop.
Get-TenantIdFromDomain
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