Public/TenantConfiguration/Get-MXRecord.ps1

function Get-MXRecord() {
    param(
        [Parameter(Mandatory)]
        [string]$TenantId,
        [Parameter(Mandatory)]
        [string]$DomainName
        )
    Connect-CustomerGraph -CustomerTenantId $TenantId

    try {
        $DomainServiceConfigurationRecords = Get-MgDomainServiceConfigurationRecord -DomainId $DomainName -ErrorAction Stop
    } catch {
        throw "Failed to get DomainServiceConfigurationRecords for domain $($DomainName): $_"
    }

    $MXRecord = ($DomainServiceConfigurationRecords | Where-Object { $_.RecordType -eq "MX" }).AdditionalProperties['mailExchange']

    if($MXRecord) {
        $MXRecord
    }
    else {
        throw "Failed to get MXRecord for domain $($DomainName), they probably do not have any Exchange licenses."
    }
}