Solved

Script to list all mx records across multiple zones

Posted on 2010-09-14
7
1,311 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

17 Experts available now in Live!

Get 1:1 Help Now