I was working with a large Exchange 2010 migration and had 50+ Accepted Domains in Exchange 2010 to take care of, many of them very old so nobody could tell me if they were in use. This company also had gone through many changes so we had to verify that each accepted domain had the correct MX record specified in the public DNS. The external domains were hosted on external DNS:es and the mailgateways were 3rd party.

So instead of manually running nslookup.exe for each domain, I created this script to automate it for me. The plan is to automate it even further so it runs once a day and e-mails me any misconfigured domain. In that way, I’m sure that all domains in Accepted Domains always are correctly configured.

In the following script I simply check that all domains in “Accepted Domains” are configured on the Internet (I use Google DNS for the query) with the following MX-records:

MX preference = 10, mail exchanger = mailgw01.domain.com
MX preference = 20, mail exchanger = mailgw02.domain.com

If they’re not, you will get an INCORRECT and the current setting output to the screen.

Any suggested changes are welcome so please comment.

The whole script:

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

$MATCH1=”MX preference = 10, mail exchanger = mailgw01.domain.com”
$MATCH2=”MX preference = 20, mail exchanger = mailgw02.domain.com”

$DNS = “”
$domains = Get-AcceptedDomain
foreach ($mxdomain in $domains) {
$NSLOOKUP = C:\Windows\system32\nslookup.exe -q=mx $mxdomain.DomainName $DNS 2>$NULL
if ($NSLOOKUP -match $MATCH1 -and $NSLOOKUP -match $MATCH2)
Write-Host Correct: $mxdomain.DomainName
else {
Write-Host =========================================
Write-Host INCORRECT: $mxdomain.DomainName -foregroundcolor “yellow”
Write-Host Output from nslookup:
Write-Host $NSLOOKUP
Write-Host =========================================

Hope you find this script useful.