OSD Task Sequence - Set Computer Name to Serial Number
SCCM OSD Task Sequence - Set Computer Name based on Serial Number
When performing 'bare-metal' builds with MDT or Configuration Manager Operating quite often in a Task Sequences you need to set the target systems computer name prior to joining the domain. The WinPE default MININT-XYZXYZ - whilst it is automatically generated will help ensure a target system completes the build without user intervention (e.g. Zero Touch) it doesn't really cut it. To keep with the zero-touch capability, one option is to use the serial number which can be easily extracted from the BIOS using a WMI query.
One of the great things about using the serial number is it doesn't rely on the presetting of something like the Asset Tag - whilst this is a very popular method of setting computer names, I personally don't like Asset Tags because when I find myself as a consultant in a larger environments (e.g. 30,000+ PCs) and I see plenty of Asset Tags values not preset. In these cases, a build will either fail (depending on your build error handling logic) or it will receive the MININT-XYZXYZ name. Long story short, using the Serial Number is what I like to call an 'assured outcome' because the serial number will always be preset, it's not reliant on a 'person' setting it when a company receives the computer and sticks an asset tag on the exterior of the computer.
There are a number of sample scripts out there which will help you set the computer name using the Serial Number extracted from WMI, this is fairly easy and straight forward. But.. there are some scenarios which are not catered for in most of the examples floating around on the Internet. For example:
- Computer names can't exceed 15 characters
- Computer names can't start with a numerical character (e.g. 0-9)
- Computer names can't contain spaces
The following script will set the computer name whilst taking into consideration the above scenarios:
On Error Resume Next
Dim objWMIService
Dim colItems
Dim colBattery
Dim objSMSEnv
Dim strNewName
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Bios")
Set colBattery = objWMIService.ExecQuery("SELECT * FROM Win32_Battery")
' Get the Serial Number
For Each objItem In colItems
' Remove all beginning, trailing and containing spaces and change to all upper case
strNewName = UCase(Trim(Replace(objItem.SerialNumber, " ", "")))
Next
' Is this a Desktop or a Laptop
If colBattery.Count = 1 Then
' Is a Laptop
strNewName = "L" & strNewName
Else
' Is a Desktop
strNewName = "W" & strNewName
End If
' If the name is longer than 15 characters, we need to truncate it
If Len(strNewName) > 15 Then
' If the name contains "-" characters (e.g. VM's), remove them
If InStr(strNewName, "-") > 0 Then
strNewName = Replace(strNewName, "-", "")
End If
' If the name is still longer than 15 characters, truncate it to first 15
strNewName = Mid(strNewName, 1, 15)
End If
' Set the Environment Variable that controls the Computer Name
Set objSMSEnv = CreateObject("Microsoft.SMS.TSEnvironment")
objSMSEnv("OSDCOMPUTERNAME") = strNewName
Set objSMSEnv = Nothing
Set colItems = Nothing
Set colBattery = Nothing
Set objWMIService = Nothing
If you're encountering issues with retrieving information from Win32_Battery from within WinPE, add a Task Sequence - Run Command Line item just before you call the Set Computer Name script, use the following Command Line:
- drvload X:\Windows\inf\Battery.inf
Feel free to use this script in your Task Sequences.