Rename A Computer Using Dell Service Tag During An System Center Configuration Manager 2007 Task Sequence

As part of a Windows 7 deployment I wanted to automate the naming of computers using the Dell service tag, prefixed with “D” for desktop, “L” for laptop and “O” for other.

You can detect the computer type using WMI and the Win32_SystemEnclosure class

strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)
Set colChassis = objWMIService.ExecQuery _
    (“Select * from Win32_SystemEnclosure”)
‘Determine the computer type
For Each objChassis in colChassis
    For  Each strChassisType in objChassis.ChassisTypes
        Select Case strChassisType
            Case 3,4,5,6,7,13,15,16,17,18,19,20,21,22,23,24
strComputerType = “D”
            Case 8,9,10,11,12,14
strComputerType = “L”
            Case Else
strComputerType = “O”
End Select
    Next
Next
Wscript.Echo strComputerType
Reading the Dell service tag is possible using WMI and the Win32_BIOS class
‘Read the Service Tag from the BIOS
Set colBIOS = objWMIService.ExecQuery _
    (“Select * from Win32_BIOS”)
For Each objBIOS in colBIOS
strComputerSerial = objBIOS.SerialNumber
Next
Wscript.Echo strComputerSerial
Renaming the computer proved to be the difficult part.  You can use WMI and the Win32_ComputerSystem class to rename a computer, but I didn’t have much success using this in the task sequence.  PowerShell and WMIC commands use the same underlying technique and also failed.  I believe the difficulty was caused by the computer being joined to the domain, rather than being a member of a workgroup.  I found a utility called WSNAME http://mystuff.clarke.co.nz/MyStuff/wsname.asp,  which is capable of renaming domain joined computers using the Windows APIs.  Unfortunately, WSNAME is not native 64bit compatible and I needed to deploy Windows 7 64bit using WinPE 64bt.
The final solution to rename the computer consists of a vbs script which detects the computer type and Dell service tag.  The new computer name is passed to WSNAME which renames the computer using a domain account with a hashed password.  Finally, the vbs script reboots the computer.  The trick to make it work is to add the vbs script to the runonce registry key through the SCCM task sequence, followed by a reboot task, which completes the task sequence.  When Windows starts following the reboot task, the vbs script runs and renames the computer.  As the vbs script is run by Windows 7, rather than WinPE, wsname.exe will work as Windows 7 64bit has WOW64 support, whereas WinPE 64bit does not.
The complete vbs script is
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
    & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2″)
Set colChassis = objWMIService.ExecQuery _
    (“Select * from Win32_SystemEnclosure”)
‘Is the computer a VM?
Set colComSys = objWMIService.ExecQuery _
    (“Select * from Win32_ComputerSystem”)
For Each objComSys in colComSys
If objComSys.Model = “Virtual Machine” Then
bolVM = True
Else
bolVM = False
End If
Next
‘Wscript.Echo bolVM
‘Determine the computer type
For Each objChassis in colChassis
    For  Each strChassisType in objChassis.ChassisTypes
        Select Case strChassisType
            Case 3,4,5,6,7,13,15,16,17,18,19,20,21,22,23,24
strComputerType = “D”
            Case 8,9,10,11,12,14
strComputerType = “L”
            Case Else
strComputerType = “O”
End Select
    Next
Next
‘Wscript.Echo strComputerType
‘Read the Service Tag from the BIOS
Set colBIOS = objWMIService.ExecQuery _
    (“Select * from Win32_BIOS”)
For Each objBIOS in colBIOS
strComputerSerial = objBIOS.SerialNumber
Next
‘Wscript.Echo strComputerSerial
‘Only rename if not a VM
If bolVM = False Then
‘Wscript.Echo “Computer name will be: ” & strComputerType & strComputerSerial
Set objShell = CreateObject(“Wscript.Shell”)
objShell.Run(“SHARE HOLDIGN WSNAME.EXE /n:” & strComputerType & strComputerSerial & ” /rcid /user:DOMAIN\USERNANE /passm:HASHED PASSWORD”)
objShell.Run(“shutdown.exe /r”)
End If
To add the vbs script to the runonce registry add a Command Line step to the task sequence and enter the command as shown in the screenshot below.  Update the command to include the appropriate path to the vbscript.
About these ads