Dynamic Computer Naming in ZTI Deployments

For a while I have been meaning to write up a solution around dynamic computer naming in Zero Touch. Many people have emailed or commenting on how useful this would be – so here goes.

Zero Touch Installation relies on a combination of System Centre Configuration Manager (ConfigMgr) and Microsoft Deployment Toolkit (MDT) 2010/2012. The MDT guys wrote a great integration tool that gives the ability to add extra functionality to ConfigMgr such as new tasks, task sequence templates and a new wizard for creating boot disks.

The integration also allows for the MDT environment to be accessed from within the ConfigMgr task sequence through the use of MDT packages that are created by an MDT task sequence wizard installed in the ConfigMgr console. This also gives the ability to access the MDT Deployment Database from within the ConfigMgr environment to set deployment variables (as opposed to setting them as collection variables).

The dynamic computer naming process I will outline in this post uses these integration points and the ability to run something called UserExit scripts which allows for the extension of the MDT inbuilt logic. The UserExit script that is attached to this post will dynamically build the computer name, but a UserExit script can be used to accomplish any task(s) you need to carry out where variables need to be combined or adapted - as the UserExit script exits, it returns its output to the task sequence process as a variable. The computer name generated by the UserExit script will be based on a location ID (two characters added as a custom property in MDT Deployment Database), the computer chassis typed (L for laptop, W for Workstation, V for Virtual and X if the chassis can not be identified), and the first 7 characters of the computer serial number (removing spaces and replacing them with minus (-) for situations where the serial number contains spaces such as virtual machines serial numbers). These three properties are then glued together and provided as the OSDComputername variable for use in the deployment process so you end up with a computer name such as: UK (location ID) L (Chassis ID) 1234567 (Serial Number ID).

For the dynamic computer naming to work, we need carry out the following task

  1. Create a new property in the MDT Deployment Database (for the two character location ID)
  2. Configure the CustomSettings.ini file (in the MDT settings package) to query the MDT Deployment Database and run the UserExit script
  3. Create the UserExit script that does the generation of the computer name and add it to the MDT Toolkit package

The instructions below assume that you have:

  • Installed MDT 2010/2012 (probably on your central site server) and setup an MDT database.
  • Run the MDT Integration (accessed through the MDT start menu folder and running Install ConfigMgr Integration)
  • Created a task sequence in ConfigMgr using the MDT integration and utilising the MDT supplied client task sequence template – if you walked through the MDT task sequence wizard you should have also created the MDT support packages (MDT Boot Image, MDT Settings, MDT Toolkit and MDT USMT packages).

Step 1 - Create a new custom property in the MDT Deployment Database (for the two character location ID)

The MDT Deployment Database provided with MDT 2010/2012 on the surface looks very simple – but is actually very powerful. In this database we can store all of the variables that we used in the MDT Lite Touch world as well as extra OSD based variables for use in Zero Touch and server build task sequences. The MDT Deployment Database contains four main areas for storing variables – Computers, Location, Roles and Make/Model. In the underlying structure of the database a separate table stores the settings that are then made visible through the DETAILS tab in each of these areas.

image

To add a custom property to the MDT Deployment Database we need to edit this table. Open your MDT Deployment Database using SQL Admin Studio and navigate to the dbo.Settings table – right click and select New Column

image

At the bottom of the column list create a new column called LocationID and set the data type to nvarchar(10) – this will give you a field of up to 10 characters (you may want to limit this to two or three characters in production depending on the naming convention you use and how many letters represent location)

image

Save the settings to the database (using the save icon on the toolbar in SQL Admin Studio) – although you have added the new container for your Location ID to the settings table – it will not be visible in the MDT database interface (within the MDT Workbench) until you open a query and execute the following stored procedures

EXECUTE sp_refreshview '[dbo].[ComputerSettings]'
EXECUTE sp_refreshview '[dbo].[LocationSettings]'
EXECUTE sp_refreshview '[dbo].[MakeModelSettings]'
EXECUTE sp_refreshview '[dbo].[RoleSettings]'

image

Your new LocationID entry point should now be visible on all of the tables in the MDT Deployment Database viewed through the Advanced Configuration\Database node in the MDT Workbench – the LocationID entry point will be at the bottom of the form in the Custom area. Now you can create a new record in the locations table based on your networks default gateways – include any location specific settings such as locale, keyboard and time zone and then add your two character location ID to your new LocationID column

image

Step 2 – Now that the MDT Deployment Database has the location part of the computer name that we need, we need to configure the CustomSettings.ini file to query the MDT Deployment Database, store the custom property added in step 1 and run the UserExit script to glue the LocationID gathered from the MDT database to a chassis ID and the adapted serial number.

When you ran the MDT task sequence wizard from within the ConfigMgr environment, you would have gone through quite a long wizard (this is accessed by right clicking the task sequence node under OSD in the ConfigMgr console and choosing “Create MDT based Task Sequence”). The wizards job is to import an MDT developed task sequence template in to the ConfigMgr environment and to walk you through creating the packages required to support this task sequence. One of the packages that is generated as part of the wizard is the MDT settings package. The settings package contains a template CustomSettings.xml file and the unattend.xml that are used throughout the execution of your chosen task sequence template.

Our first task is to replace the template CustomSettings.ini file in your settings package with one that contains the MDT database queries that we want to run to pull in the LocationID (and any other settings you have added to the location record). This can be achieved by right clicking the database node in the MDT Workbench and running the Configure Database Rules wizard. This wizard will help to create the queries for each of the tables in the MDT database. In this post we are interested in the LOCATION options, however you can choose any queries to be added to Customsettings.ini if you are using other MDT variables.

Deselect the options in the COMPUTER options and click next. On the LOCATIONS options, adjust so that only the first two options are ticked.

image

You can deselect all of the queries on the MAKE/MODEL options and the ROLE options (if they are not needed) and click through to the end and select Finish. This will update the CustomSettings.ini file stored in the Control folder of your deployment share – open this folder and open the CustomSettings.ini file. It should look like the example below with the MDT Deployment Database queries added.

image

The next task is to adjust the CustomSettings,ini file to deal with the new LocationID value and to run the UserExit script that will create the OSDComputerName. With the newly adjusted CustomSettings.ini file open add the following lines to the file.

In the [Settings] section on the Priority line add BuildComputerName to the end of the line (after all of the database entries but before Default) 

It line should now read Priority=Locations, LSettings, BuildComputerName, Default  - you may have more queries on this line if you left additional queries ticked in the Configure Database Rules wizard.

in the [Settings] section on the Properties line add BuildComputerName and LocationID after MyCustomProperty 

this line should now read Properties=MyCustomProperty, BuildComputerName, LocationID

Create a new section under the Properties line called [BuildComputerName] and configure as follows:

[BuildComputerName]
UserExit=MachineNameExit.vbs
OSDComputerName=#BuildComputerName()#

Your CustomSettings.ini file should now look something like this (but containing the connection information for your MDT Deployment Database environment):

image

Save your newly edited CustomSettings.ini file and then copy this file (from the control folder) to the source location of your MDT Setting Package – The source location is the folder that would have been set during the MDT Wizard in ConfigMgr and is the location where the MDT Settings Package get its source files from – replace the CustomSettings.ini file in the source folder and then replicate the settings package to your distribution points in ConfigMgr.

image

Step 3 – Now that the MDT database is set, and the CustomSettings.ini file is configured to query the MDT Deployment Database and run the UserExit script, the final piece of the jigsaw is to add the UserExit script itself to the MDT Toolkit Package (again this should have been created when you setup your task sequence using the integrated MDT task sequence template and the package wizard). Open the source folder where your MDT Toolkit Package is stored and add the MachineNameExit.vbs file attached to this post into the SCRIPTS folder

image

Replicate your MDT Toolkit Package (with this new file added) around to your ConfigMgr distribution points using the ConfigMgr console.

Outcome

When the task sequence runs, the normal gather process that MDT uses will poulate the deployment variables with environmental information about the device being built as well as variables queried from the MDT database (as setup in the queries in CustomSettings.ini). The sample script provided will then be run as a UserExit funtion which in turn will run a function called BuildComputerName. This function has all of the logic to obtain gathered variables found during the gather process. The variables we are interested in are:

oEnvironment.Item("LocationID")
oEnvironment.Item("isLaptop")
oEnvironment.Item("isDesktop")
oEnvironment.Item("isVM")
oEnvironment.Item("SerialNumber")

The script then glues the pieces together as follows:

Character 1 and 2 - Letters pulled from the MDT database LocationID attribute during the gather process (this will be XX if nothing is set in the database)
Character 3 – A single letter based on the chassis of the device which was discovered in the gather process (L for laptop, W for Workstation, V for Virtual and X if the variable is empty)
Character 4 to 10 – The last seven characters of the serial number (recorded in the gather process) of the machine with spaces replaced with dash (-)  The replacement of spaces is required because some virtual machines tend to have the serial number set to a GUID with spaces - the script takes care of dealing with this.

NOTE - You could change from using the serial number to using the machines asset tag (if this has been set in the BIOS) by changing the code in the script to get the asset tag attribute instead of SerialNumber – to do this simply change the line

sSerialNumber = oEnvironment.Item("SerialNumber") to sSerialNumber = oEnvironment.Item("AssetTag")

The script then passes back the compiled computer name to the UserExit as BuildComputerName which is then converted and stored as OSDComputerName for use later in the task sequence

This post was contributed by Richard Smith, a Principal Consultant with Microsoft Services UK

Disclaimer: The information on this site is provided "AS IS" with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use

MachineNameExit.zip