Solved

Script to list all mx records across multiple zones

Posted on 2010-09-14
7
1,325 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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