2way Technology

Configuration Manager, Windows 7 OSD, Scripting, Application Packaging, Windows Embedded, Microsoft Surface

Recent posts

Tags

Categories

Navigation

Pages

    Archive

    Blogroll

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    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.

    Posted: Jan 21 2011, 23:43 by Ben Fisher | Comments (3) RSS comment feed |
    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Filed under: OSD | SCCM 2007 | SCCM 2012

    Comments

    Andres Estonia said:

    AndresHi,

    I found what why win32_battery class was not working. It needs special windows driver to recognize battery. If extra drivers are added to WinPE boot image then it works.

    # February 08 2011, 18:02

    Andres Estonia said:

    AndresThere is also possibility to use Win32_PortableBattery win32 class.

    # February 09 2011, 03:58

    applepie.se said:

    pingbackPingback from applepie.se

    Automatically generate name using ConfigMgr / MDT | The knack

    # December 26 2011, 18:20
    Comments are closed