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 contoso.com“)

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

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(“http://schemas.microsoft.com/cdo/configuration/sendusing“) = cdoSendUsingPort
 .Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver“) = strSmartHost
 .Item(“http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout“) = 10
 .Update
 End With

‘ build HTML for message body

 strMSG = “Record changed”

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

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

End Sub