Public/func_Get-TenantIdFromDomain.ps1

<#
.SYNOPSIS
    A function I got from the Internet that returns the tenantID when provided with a domain.
 
.NOTES
    Author: Someone on the Internet
    Version: 2023.03.13.2300
 
.CHANGELOG
    2023.03.13.1600 - Initial Version of this as a func
    2023.03.13.2300 - Added the Process block for advanced functions
#>


function Get-TenantIdFromDomain {
    [CmdletBinding()]
    param(
        [Parameter(Position = 0, Mandatory = $true)]
        [string]$FQDN
    )

    Process{

        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
        }
    
    }
}