Script: DNS Lookup in the Task Sequence

I recently had a need during a server deployment project to have the ability to check the IP address for a server and the hostname in the UDI wizard for a valid DNS entry. I ended up writing a script to do this so thought I would share.


In the server VLAN IP addresses are statically assigned by the network team in a third-party DNS system. As part of the pre build process information is provided as the task sequence starts where the user enters the IP address of the server they are building. The UDI wizard then launches up where the administrator then configures the server and the software required and then the build continues.

Right after the UDI wizard was completed I wanted a way to check the IP address the user entered and make sure it returned the hostname of the server from DNS that they entered into the UDI wizard. I could have done this a few ways. The first is to create a preflight check in the UDI wizard and move the preflight deployment checks page till later in the UDI wizard (right before the summary page). The other was to create a custom form in the UDI wizard using the SDK however I have no C++ knowledge so this would have taken too long. I went with what I thought was the simpler option which is to just create a custom MDT script which you put right after the UDI wizard.

How The Script Works

The script works by using nslookup.exe, passing the IP address of the server to nslookup, redirecting the output of the command into the script, splitting the returned output to look for the “Name:” portion of the returned text and then matching the name of the server the user provided with the one returned by the script. If everything matches up we return exit code 0 and everything is happy. If we have a mismatch we return a custom error code and the task sequence fails.

The Script…

Ok so not the band but here it is, save this in your scripts folder in your Deployment Toolkit package, update your distribution points, then just call the script with a Run Command Line task sequence step. Hold on I hear you say, what about nslookup? You are correct, you can inject nslookup.exe into your boot image using the include extras option when you create your boot image, then it will be available in the boot image when it loads.

<job id="input">
   <script language="VBScript" src="ZTIUtility.vbs"/>
   <script language="VBScript"> 
      '//  Global constant and variable declarations
      Option Explicit 
      Dim iRetVal
      '//  End declarations
      '//  Main routine
      On Error Resume Next
      iRetVal = ValidateDNS
      ProcessResults iRetVal
      On Error Goto 0
      '//  Function:  ValidateDNS()
      '//  Input:     None
      '//  Return:    Success – 0
      '//             Failure – non-zero
      '//  Purpose:   Performs checking of DNS records before deployment
      Function ValidateDNS()
         Dim objShell, objExec, sResults, aNameTemp, aName
         Set objShell = CreateObject("WScript.Shell")
         Set objExec = objShell.Exec("nslookup.exe " & oEnvironment.Item("CustomVariable_IPAddress"))
         Do Until objExec.Status = 1
            WScript.Sleep 100
         sResults = objExec.StdOut.Readall
         aNameTemp = Split(sResults, "Name:")
         aName = Split(Trim(aNameTemp(1)), Chr(13))
         If aName(0) = oEnvironment.Item("OSDComputerName") & "" Then
            ValidateDNS = 0
            ValidateDNS = 5896789
         End If
         Set objExec = Nothing
         Set objShell = Nothing
      End Function 

Well that is about it, when you call your script you can use the DeployRoot variable like this:

cscript.exe %deployroot%/scripts/Z-ValidateDNS.wsf

Hope everyone finds this helpful, I intend to make this into a preflight check for the UDI wizard at some point as well so stay tuned if you prefer that method.


Tags: , , , , , ,

About Martyn

Martyn is one of the Senior Cloud Architects and DevOps Team Leader at one of the worlds leading Cloud Transformation Specialists Inframon. Martyn is responsible for the architecture of some of the largest Azure deployments in EMEA and is a advisor to a many businesses on their strategies. Martyn is a regular speaker at Microsoft events and community events on Azure and DevOps, giving his insight to a growing number of audiences.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: