?
Solved

Script to list all mx records across multiple zones

Posted on 2010-09-14
7
Medium Priority
?
1,383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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