Solved

Script to list all mx records across multiple zones

Posted on 2010-09-14
7
1,316 Views
Last Modified: 2012-06-21
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!
0
Comment
Question by:exadmin2006
  • 4
  • 2
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33676924
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
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33676946
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

0
 

Author Comment

by:exadmin2006
ID: 33676953
Great! I will try this tomorrow and let you know.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 17

Expert Comment

by:Tony Massa
ID: 33677228
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

0
 

Author Comment

by:exadmin2006
ID: 33683429
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.
0
 
LVL 17

Expert Comment

by:Tony Massa
ID: 33683531
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"
0
 
LVL 17

Accepted Solution

by:
Tony Massa earned 500 total points
ID: 33683562
Since you're looking for a specific record, then use this script, and customize line 5:
Const ForReading = 1, ForWriting = 2, ForAppending = 8

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

strFindRecord = "mail1.domain.com"

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
  	If objRecord.MailExchange = strFindRecord Then
      objResults.Writeline objRecord.DomainName & vbTab & objRecord.MailExchange
    End If
  Next
Next

objResults.Close
WScript.Quit

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now