MDT Scripting: Using VBScript Expressions in customsettings.ini file

I received an email from one of my readers:

I’m trying to deploy to a mixed physical / VM environment using the serial number for the OSDComputername. However, on my VMs (VM Ware) the VM serial number is full of spaces.  is there a way to stripe out the spaces on the fly?

Hi there, the answer is yes, there’s a number of ways to tackle this issue so I’ll walk through how I dealt with it.

To begin with, boot into WinPE and take a look at the problem itself, the serial number.

serialnumber

As you can see, it’s a messy one about 40 characters in length, it has leading text, spaces, 2 hyphen and although you can’t see it trailing spaces. VBScript has a number of ways to reformat strings.

If you’ve read any of my scripting articles you will know that you can get a copy of the MDT scripting template and add the following line to output an MDT variable to the console:

wscript.echo oEnvironment.Item("SerialNumber")

Your full code should look like this example:


<job id="Z-Sample">
<script language="VBScript" src="ZTIUtility.vbs"/>
<script language="VBScript">

' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution: Solution Accelerator for Microsoft Deployment
' // File: ZTIComputername.wsf
' //
' // Purpose: Testing MDT Variable Output
' //
' // Usage: cscript ZTIComputername.wsf [/debug:true]
' //
' // Script Version: 1.0.0
' // History:
' //
' // ***** End Header *****
' //***************************************************************************

'//----------------------------------------------------------------------------
'//
'// Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

Option Explicit

Dim iRetVal

'//----------------------------------------------------------------------------
'// End declarations
'//----------------------------------------------------------------------------

'//----------------------------------------------------------------------------
'// Main routine
'//----------------------------------------------------------------------------

On Error Resume Next
iRetVal = ZTIProcess
ProcessResults iRetVal
On Error Goto 0

'//---------------------------------------------------------------------------
'//
'// Function: ZTIProcess()
'//
'// Input: None
'//
'// Return: Success - 0
'// Failure - non-zero
'//
'// Purpose: Perform main ZTI processing
'//
'//---------------------------------------------------------------------------
Function ZTIProcess()

iRetVal = Success

ZTIProcess = iRetval

     wscript.echo oEnvironment.Item("SerialNumber")

End Function

</script>
</job>

Save your script to the DeploymentShare\scripts folder and name it ZTIComputername.wsf. Next, boot your machine from your Lite-Touch media. Then in WinPE navigate to Z:\scripts and type:

cscript ZTIComputername.wsf

This is now the environment that we’ll use to customise the variable and test the results.

Echo serialnumber

Now we need to work on it.

OK, first customise your script with this code below. This is a basic script that outputs both the old and new serial number. I’ve shown the whole function this time to demonstrate where to insert the code in the template.

I’m using the VBScript replace function to remove all the spaces.

Function ZTIProcess()
     iRetVal = Success
     ZTIProcess = iRetval

	wscript.echo vbCRLF & vbCRLF
	wscript.echo "Original SerialNumber: " & oEnvironment.Item("SerialNumber")
	wscript.echo vbCRLF & vbCRLF
	wscript.echo Replace(oEnvironment.Item("SerialNumber")," ","")
	wscript.echo vbCRLF & vbCRLF

End Function

Run the script again and this time the output looks like this (No Spaces):

Replace Spaces

For the next step. Let’s get rid of the hyphens by using the same method. You can simply wrap the existing code in the same method.

	wscript.echo Replace(Replace(oEnvironment.Item("SerialNumber")," ",""),"-","")

Replace Hyphens

Looking good, now let’s strip away the word ‘VMware’ from the start (First 6 charactars, plus 1) then select only 15 characters, the maximum allowed length for a computername.

  	wscript.echo Mid(Replace(Replace(oEnvironment.Item("SerialNumber")," ",""),"-",""),7,15)

Mid Text

Perfect. Now, the question is does that mean you have to write a vbscript to create the computername? No, you can use the expression you created above in the customsettings.ini enclosed in hashes. Here’s the finished solution(it’s a whopper).

OSDComputerName=#Mid(Replace(Replace(oEnvironment.Item("SerialNumber")," ",""),"-",""),7,15)#

Now in the deployment wizard, this is what you’ll see:

Results

There are many other ways of manipulating text in VB. There’s an MSDN manual here. You should be able to use these methods with other variables to tackle further issues.

About these ads

About Andrew Barnes

A Scipting and Deployment Specialist.
This entry was posted in MDT 2010, MDT 2012, Scripting and tagged , , , . Bookmark the permalink.

8 Responses to MDT Scripting: Using VBScript Expressions in customsettings.ini file

  1. Alexandr says:

    For Hyper-V Virtual machines, i just corrected xml file,this part whose consist serial number, when i creating Virtual PC, serial number is more than 40 symbols
    77-88-99

    Like

  2. Nick says:

    Is there an easy way to ensure the length of a the OSDComputer name is less than 15 characters? We have a set text string of 8 characters plus the asset tag value for OSDComputername “XXX-XXX-%assettag%” I’m running into issues where if the asset tag is not defined, it results in the litteral text xxx-xxx-%assettag% or the variable length is too long(>7 char for tag) , I get either a unattend.xml error in Windows 7, or have a stop in the unattended minisetup for XP.
    Is it possible to use a left command on the already defined osdcomputername to limit it to 15 characters? or would defining it as below actually work?

    osdcomputername=xxx-xxx-#left(oEnvironment.Item(“SerialNumber”),7)#

    Also is it possible to trap the error if %assettag% is undefined?

    Like

    • I would create a custom VBScript to handle that. You could code in the logic to detect if the asset tag is not defined and deal with it. You could also factor in any error trapping scenarios.

      Like

      • Nick says:

        I ended up creating a custom variable in the gather.xml and checked its value if it was blank or more than 7 character it defined it so my techs would notice, or it truncated the variable it it was more than 7 characters and defined the osdcomputername as my text plus the new variable. Thanks for the idea.

        Like

  3. Reza Daniels says:

    Hi there

    When i replace SerialNumber with MACAddress, i receive an error messages, is there a way to get a list of expressions that one can use in the customsettings.ini file

    OSDComputerName=#Mid(Replace(Replace(oEnvironment.Item(“MACAddress”),” “,””),”:”,””),7,15)#

    Like

  4. G says:

    I tried out the script as shown, with the edits. It shows a result with the properly-concatenated serial number, but it does not set this output as a variable (such as SerialNumber or something else that c. How would this be done?

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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