PowerShell - MX Record Report Using Accepted Domains

I put this together so I can watch my DNS Records from falling off to someone elses hands. I had to add all of them to accepted domains and specify my MX records name to match against. Its really helpful and i have added it to Task Scheduler to run every day and email me or a group.


Param (

       [Parameter(Position = 1, Mandatory = $False)]
       [String] $From = "DNS.Alerts@DOMAIN.com",
       [Parameter(Position = 2, Mandatory = $False)]
       [String] $To = "USER@DOMAIN.COM",
       [Parameter(Position = 3, Mandatory = $False)]
       [String] $Subject = "External NSLookup Accepted Domains",
       [Parameter(Position = 4, Mandatory = $False)]
       [String] $SMTPserver = "SMTPSERVER.DOMAIN.com"

if ( (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue) -eq $null )
 Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

[Array] $Domaincol = @()

# Match with OUR MX Records

$MATCH1=”MX preference = 10, mail exchanger = PLACE MX RECORD HERE (1)”
$MATCH2=”MX preference = 20, mail exchanger = PLACE MX RECORD HERE (2)”

# Pull our Accepted Domains from Exchange and do a nslookup for each one using google for the dns server (outside)
$DNS = “”
$domains = Get-AcceptedDomain
foreach ($mxdomain in $domains) {
 $NSLOOKUP = C:\Windows\system32\nslookup.exe -q=mx $mxdomain.DomainName $DNS 2>$NULL
 $Domainobj = New-Object PSObject -Property @{
    Domain = $mxdomain.DomainName
    NSLookup = $NSLOOKUP
    $Domaincol += $Domainobj

#Set the

$Domaincol | Sort Name | FT -Auto

       $HTMLtableHeader1 = "#002C54" # Dark Blue
       $HTMLtableHeader2 = "#325777" # Lighter Blue

       $reportBody = "<!DOCTYPE html>

                                  body {
                                         font-size: 10pt;
                                         background-color: white;
                                         color: #000000;

                                  table {
                                         border: 0px;
                                         border-collapse: separate;
                                         padding: 3px;

                                  .highlight_red { background-color: red; }

                                  tr, td, th { padding: 3px; }

                                  th {
                                         color: #FFFFFF;
                                         font-weight: bold;
                                         text-align: center;

                                  h1,h2,h3,h4,h5 { color: $HTMLtableHeader1; }

       $reportBody += "<BODY>
                                  <h2 align=""center"">External NSLookup from Accepted Domains</h2>
                                  <h4 align=""center"">$((Get-Date).ToString())</h4>
                                  </font> <br>"

       $reportBody += "<table>
                                  <tr bgcolor=""$HTMLtableHeader1""><th>Domain</th><th>NSLookup</th></tr>"

       $greyRow = $False

ForEach ($DM in ($Domaincol | Sort Name))

              $reportBody += "<tr"
              If ($greyRow) { $reportBody += " style=""background-color:#dddddd"""; $greyRow = $False }
              Else { $greyRow = $True }

              $reportBody += "><td>$($DM.Domain)</td>"

              If ($dm.NSLOOKUP -match $MATCH1 -and $dm.NSLOOKUP -match $MATCH2 -or $dm.domain -match $Match3-or $dm.domain -match $Match4) {$reportBody += "<td>$($DM.NSLOOKUP)</td>"}
              Else {$reportBody += "<td class=""highlight_red"">$($DM.NSLOOKUP)</td>"}

              $reportBody += "</tr>"

       $reportBody += "</table><br>"
       $reportBody += "</BODY> </HTML>"

       Send-MailMessage -From $From -To $To -Subject $Subject -Body $reportBody -BodyAsHtml -SMTPserver $SMTPserver -Priority High



  1. Hi, sorry for commenting on non so relevant post.
    I found your question/comment on technet from Tuesday, October 17, 2017 2:25 PM

    How that turn out? I have the same situation with primary mailbox on premise and archive in the cloud with 9x GB so far. Will it autoexpand ? :)

    I too am having the same problem in my Org. How to I change the archive quota in a hybrid environment without moving the users primary mailbox to O365?

    I have opened several support tickets with Microsoft and they just take me for loops and gather information for them each time i spend 2 hours on the phone with them. Huge waste of time and still have no results.


Post a Comment

Popular Posts