Link to home
Start Free TrialLog in
Avatar of exadmin2006
exadmin2006Flag for United States of America

asked on

Script to list all mx records across multiple zones

Hey all,

We have a Server 2003 server with DNS that runs around 790 zones. I have been asked to find out which of these zones has a specific MX record. Is there a script option here that will query all 790 zones for a particular MX record (by name, like mail.company.com). Thanks!
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Well with Batch you can get using nslookup the MX records, e.g.

NSLOOKUP -type=MX domain.co.uk. 2>NUL | FIND "MX"

Now you could run a text file of the domains through that, e.g.

@echo off
REM Check MX records of each domain listed in text file domainlist.txt
REM Report all MX records back into log.txt and any that contain the word
REM messagelabs into the file found.txt

set log=log.txt
del %log%

set find=messagelabs
set found=found.txt

for /f "tokens=*" %%a in ('type domainlist.txt') do (
  echo Checking "%%a."
  nslookup -type=MX %%a. 2>NUL | find "MX" >> %log%
)

start "Full list" notepad %log%

find /i "%find%" < %log% >%found%
start "Part list" notepad %found%

hth

Steve
This will get all of the MX records on your DNS server.  Unfortunately, it will also pull cached MX records for external domains.  If you clear your DNS cache, this may be more helpful.  If the server you run it on isn't a caching server, it should work just fine.

http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/dns/records/#LMEDNSR.htm
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\root\MicrosoftDNS")

Set colItems = objWMIService.ExecQuery("Select domainName, mailExchange from MicrosoftDNS_MXType")

For Each objItem in colItems
    Wscript.Echo "Domain Name: " & objItem.DomainName
    Wscript.Echo "Mail Exchange: " & objItem.MailExchange
    Wscript.Echo
Next

Open in new window

Avatar of exadmin2006

ASKER

Great! I will try this tomorrow and let you know.
I've updated the attached script to dynamically pull only the 'hosted' domains and ignore any reverse lookup domains as well.  The results will be in a Tab-Delimited file called MXRecords.txt
Const ForReading = 1, ForWriting = 2, ForAppending = 8

strComputer = "."   'Or Use DNS Server Name if Running Remotely

Set fso = CreateObject("Scripting.FileSystemObject")
Set objResults = fso.CreateTextFile("MXRecords.txt", ForWriting)

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & _
        "\root\MicrosoftDNS")

Set colItems = objWMIService.ExecQuery("Select * from MicrosoftDNS_Zone where NOT Name like '%in-addr%'")

For Each objItem in colItems
  strDomain = objItem.Name
  Set colRecords = objWMIService.ExecQuery("Select domainName,mailExchange from MicrosoftDNS_MXType where DomainName='" & strDomain & "'")
  For Each objRecord in colRecords
    objResults.Writeline objRecord.DomainName & vbTab & objRecord.MailExchange
  Next
Next

objResults.Close
WScript.Quit

Open in new window

Thanks. I don't know scripting much at all..in your script above, what variables do I need to customize? I assume somewhere in there I need to put the MX record I am looking for? Thanks.
It will pull all MX records in every zone.

Run the script on your DNS server...that should be it.  It will output a TSV formatted text file that you can import (or copy/paste)  into Excel.

If you want to run the script from a different computer with and admin account, you would only have to change line 3:

strComputer = "."

Change to

strComputer = "dns-server.yourdomain.net"
ASKER CERTIFIED SOLUTION
Avatar of Tony Massa
Tony Massa
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial