Public/Utilities/Get-M365Records.ps1

function Get-M365Records () {
    Param(
        [Parameter(Mandatory)]
        [string]$TenantId
    )


    try {
        Connect-CustomerGraph -CustomerTenantId $TenantId

        $Domains = Get-MgDomain | Where-Object { $_.IsInitial -eq $false }

        $SelectedDomains = $Domains | Out-ConsoleGridView -Title "Select domains to check" -OutputMode Multiple

        $SelectedDomains | ForEach-Object {

            $Domain = $_
            $Records = Get-MgDomainServiceConfigurationRecord -DomainId $($Domain.Id)


            $NameServer = Resolve-DnsName -Name $($Domain.Id) -Type ns -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty NameHost

            if ( $NameServer -like '*.curanet.dk' ) {
                $OurCuranet = Get-CuraDNSRecords -DomainName $Domain.Id
                if ( $OurCuranet.status -ne 404 ) {
                    $OurCuranet = $true
                }
                else {
                    $OurCuranet = $false
                }
            }

            Write-Host "[$($Domain.Id)] NS: $NameServer" -NoNewline
            if ( $OurCuranet ) {
                Write-Host " (3370)" -ForegroundColor Green
            } else {
                Write-Host " (Other)" -ForegroundColor Red
            }

            $Color = "Red"
            $MXRecord = ($Records | Where-Object { $_.RecordType -eq "MX" }).AdditionalProperties.mailExchange
            $CurrentRecord = Resolve-DnsName -Name $($Domain.Id) -Type mx -ErrorAction SilentlyContinue | Where-Object Type -eq 'MX' | Sort-Object Preference | Select-Object -First 1

            if ($CurrentRecord) {
                if ( $CurrentRecord.NameExchange -eq $MXRecord) {
                    $Color = "Green"
                }
            }
            Write-Host "[$($Domain.Id)] MX: $MXRecord" -ForegroundColor $Color

            $AutodicoverRecord = ($Records | Where-Object { $_.Label -like "Autodiscover*" }).AdditionalProperties.canonicalName
            $Color = "Red"
            $CurrentRecord = Resolve-DnsName -Name "autodiscover.$($Domain.Id)" -Type cname -ErrorAction SilentlyContinue

            if ($CurrentRecord) {
                if ( $CurrentRecord.NameHost -eq $AutodicoverRecord) {
                    $Color = "Green"
                }
            }
            Write-Host "[autodiscover.$($Domain.Id)] CNAME: $AutodicoverRecord" -ForegroundColor $Color

            $Color = "Red"
            $SPFRecord = (($Records | Where-Object { $_.RecordType -eq "TXT" }).AdditionalProperties.text -split ' ')[1]
            $CurrentRecord = Resolve-DnsName -Name "emas-int.com" -Type txt -ErrorAction SilentlyContinue | Where-Object Strings -like 'v=spf1*' | Select-Object -ExpandProperty Strings

            if ($CurrentRecord) {
                if ( $CurrentRecord -like "*$($SPFRecord)*") {
                    $Color = "Green"
                }
            }
            Write-Host "[$($Domain.Id)] SPF: $SPFRecord" -ForegroundColor $Color

            $Color = "Red"
            $EnterpriseRegistration = ($Records | Where-Object { $_.Label -like "EnterpriseRegistration*" }).AdditionalProperties.canonicalName
            $CurrentRecord = Resolve-DnsName -Name "enterpriseregistration.$($Domain.Id)" -Type cname -ErrorAction SilentlyContinue

            if ($CurrentRecord) {
                if ( $CurrentRecord.NameHost -eq $EnterpriseRegistration) {
                    $Color = "Green"
                }
            }
            Write-Host "[enterpriseregistration.$($Domain.Id)] CNAME: $EnterpriseRegistration" -ForegroundColor $Color

            $Color = "Red"
            $EnterpriseEnrollment = ($Records | Where-Object { $_.Label -like "EnterpriseEnrollment*" }).AdditionalProperties.canonicalName
            $CurrentRecord = Resolve-DnsName -Name "enterpriseenrollment.$($Domain.Id)" -Type cname -ErrorAction SilentlyContinue

            if ($CurrentRecord) {
                if ( ($CurrentRecord.NameHost -eq $EnterpriseEnrollment) -or ($CurrentRecord.NameHost -eq "enterpriseenrollment.manage.microsoft.com")) {
                    $Color = "Green"
                }
            }
            Write-Host "[enterpriseenrollment.$($Domain.Id)] CNAME: $EnterpriseEnrollment" -ForegroundColor $Color

        }
    }
    catch {
        throw "Failed to get domain records on $($TenantId): $_"
    }
}