Created
January 15, 2025 00:01
-
-
Save domkirby/20238250b8b50023bdf8f09af4e9e221 to your computer and use it in GitHub Desktop.
Uses openid /.well-known on Entra to look for a Tenant ID
This file contains hidden or 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-TenantIdFromDomainName { | |
param ( | |
[Parameter(Mandatory = $true)] | |
[ValidatePattern('^([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$')] | |
[string]$DomainName | |
) | |
try { | |
# Construct the URL | |
$domLookupUrl = "https://login.microsoftonline.com/$DomainName/.well-known/openid-configuration" | |
# Fetch the configuration | |
$response = Invoke-RestMethod -Uri $domLookupUrl -Method Get -ErrorAction SilentlyContinue | |
# Parse the tenant ID from the authorization endpoint | |
$authorizationEndpoint = $response.authorization_endpoint | |
if ($authorizationEndpoint -match '/([a-f0-9-]{36})/') { | |
$tenantId = $matches[1] | |
return $tenantId | |
} else { | |
throw "Tenant ID not found in the authorization endpoint." | |
} | |
} catch { | |
Write-Warning "An error occurred: $_" | |
return $null | |
} | |
} | |
#Example Usage | |
$domainName = Read-Host -Prompt "Enter Domain Name" | |
Write-Progress -Activity "Extracting Tenant ID" -Status "Please wait..." -PercentComplete 50; | |
$tenantId = Get-TenantIdFromDomainName -DomainName $domainName; | |
if($tenantId) { | |
Write-Progress -Activity "Extracting Tenant ID" -Status "Tenant ID extracted successfully." -Completed; | |
Write-Host "Tenant ID extracted from domain name: $tenantId" -ForegroundColor Green; | |
} else { | |
Write-Progress -Activity "Extracting Tenant ID" -Status "Failed to extract Tenant ID." -Completed; | |
Write-Host "Failed to extract Tenant ID from domain name $domainName. There may be no tenant at this domain, verify your domain name. Exiting script." -ForegroundColor Red; | |
pause; | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment