
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.

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 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 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 customise the variable and test the results.

Now we need to work on it.
To remove all spaces from a string
Ok, now you customise you 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.
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:

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")," ",""),"-","")

Looking good, now let’s strip away the word ‘VMware’ at the start and select only 15 characters as 15 is the maximum allowed length.
wscript.echo Mid(Replace(Replace(oEnvironment.Item("SerialNumber")," ",""),"-",""),7,15)

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:

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.