Naming machine using script

While deploying machine using MDT or SCCM a lot of situation comes where you need to rename the machine automatically.

This is the script which will name your computer automatically :-

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

It quires for the machine serial number and name the machine accordingly.if the machine is a desktop you will get a W before the computer name & if it's a laptop you will get a L before the computer name.

using this script with MDT :-



set the Task Sequence Variable as OSDComputerName.

using the script with SCCM:-
In a similar way you can use this script with SCCM you just need to create a package for the script.
Data Source of the package will be upto the script name.
use the script in the SCCM TS with Task Sequence Variable set as OSDComputerName.
the machine will be named something like this :-

4 comments:

  1. I am building a DELL E5420 and it registers the machine as a Workstation. Why is that? All the research points to the same script.

    ReplyDelete
  2. hi,
    This script just checks for the battery presence if it's a laptop it will have a battery & name the machine accordingly...
    This is defined here :-
    If colBattery.Count = 1 Then
    ' Is a Laptop
    strNewName = "L" & strNewName
    Else
    ' Is a Desktop
    strNewName = "W" & strNewName
    End If

    are you using the same script for machine naming ??

    ReplyDelete
  3. Hi Pranay,

    I am using this script in my TS to name the laptops and desktops accordingly. Do you think the Dell battery registers as more than 1? It is a 9-cell extended battery.

    ReplyDelete
  4. 1 is just for true or false if it's 1 then the battery is present there...
    Set colBattery = objWMIService.ExecQuery("SELECT * FROM Win32_Battery")
    the above command from the script queries for battery presence and set the value in colBattery accordingly if the battery is present it will set it to 1 else 0.
    In case of confusion just add a MsgBox "colBattery" it will display you the value 0 or 1.i have never tried this on a 9-cell battery but i think it wont make any difference.try to put a MsgBox and get the value out...Please post the results...

    ReplyDelete