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.

ENJOY!



<!--
[CmdletBinding()]
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)”
$Match3="EXCLUDE.mail.onmicrosoft.com"
$MATCH4="EXCLUDE THIS ONE TOO FOR EXTERANL CONTACTS"

# Pull our Accepted Domains from Exchange and do a nslookup for each one using google for the dns server (outside)
$DNS = “8.8.8.8”
$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>
                                  <HTML>
                                  <head>

                                  <style>
                                  body {
                                         font-family:Verdana,Arial,sans-serif;
                                         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; }
                                  </style></head>"

       $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



-->



Comments

Popular Posts