Monitoring for MX record changes with VBScript

Sometimes, like in the case of an email migration, it is necessary to wait for DNS changes (MX records for example) to replicate before continuing your migration, otherwise mail might bounce or other nastiness may occur.

The following VB Script runs in a loop and monitors a DNS record, in this case an MX record, for an expected new value.  Once the value is returned the script will send an email.   This is great if you want to make an MX record change, then go to dinner while you wait for your ISP’s changes to replicate, then send you an email once it’s returning the correct value.

Simply change the top 2 highlighted entries to identify the MX record value you expect from the NSLOOKUP, and the other highlighted portions to define the SMTP relay you should use, reply-to address and recipient email.


Set oShell = CreateObject(“Wscript.Shell”)

Set oExec = oShell.Exec(“nslookup -type=mx“)

  Dim CheckStatus
  CheckStatus = oExec.Status
  Do Until oExec.StdOut.AtEndOfStream
   strLine = oExec.StdOut.ReadLine()
   If InStr (strLine,”MX preference”) <> 0 Then
     If InStr (strLine,”“) <> 0 Then
       strMXChanged = “MX record for has changed”
       SendMail ““, strMXChanged
       Exit Do
     End If
    End If
  If CheckStatus <> 0 Then Exit Do

Sub SendMail(strTO, strMXChanged)

 Dim iMsg
 Dim iConf
 Dim Flds
 Dim strHTML
 Dim strSmartHost

 Const cdoSendUsingPort = 2
 StrSmartHost = “mail.contoso.corp

 set iMsg = CreateObject(“CDO.Message”)
 set iConf = CreateObject(“CDO.Configuration”)

 Set Flds = iConf.Fields

‘ set the CDOSYS configuration fields to use port 25 on the SMTP server

 With Flds
 .Item(““) = cdoSendUsingPort
 .Item(““) = strSmartHost
 .Item(““) = 10
 End With

‘ build HTML for message body

 strMSG = “Record changed”

 ‘ apply the settings to the message
 With iMsg
 Set .Configuration = iConf
 .To = strTO
 .From = “
 .Subject = strMXChanged
 .TEXTBody = strMSG
 End With

‘ cleanup of variables
 Set iMsg = Nothing
 Set iConf = Nothing
 Set Flds = Nothing

End Sub

Comments (0)

Skip to main content