Name computer with serial # and join to domain?

Posted on 2011-04-19
Last Modified: 2012-05-11
Does anyone have a working script or know of a way to script VBScript or Batch file that will do the following:

1. Name the computer with three parts to the name separated by a hyphen (- sign): three letter city code - the letters "LP" - then the service tag (it's a Dell). So the example computername would be HOU-LP-ServiceTag (where ServiceTag is the the 7 character serial like most Dells have.
2. Then join the system to the domain, AND POSSIBLY put it into the OU automatically (OU=Laptop,OU=Houston,OU=Initech Workstations,DC=Initech,DC=com).

I have this script below which is a script that successfully gets the service tag/serial number, but it merely inputs it into the clipboard (so you can go paste it somewhere).
I am not the author behind the script, do not have the expertise to manipulate it with the automation parts.
strComputer = InputBox("Enter a computer name to get service tag")
If IsEmpty(strComputer) Then
    MsgBox "Cancelled", vbExclamation, "Cancel Pressed"
ElseIf Len(strComputer) = 0 Then
    MsgBox "You did not enter a PC name.", vbInformation, "OK pressed"
    Set objWMIService = GetObject("winmgmts:" & _
            "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    For Each objSMBIOS In objWMIService.ExecQuery("Select * from Win32_SystemEnclosure") 
        strSerialNumber = objSMBIOS.SerialNumber
        Set objIE = CreateObject("InternetExplorer.Application")
        objIE.document.parentwindow.clipboardData.SetData "text", strSerialNumber
        Wscript.Echo "The service tag for " & strComputer & " is: " & strSerialNumber
End If

Open in new window

Question by:garryshape
    LVL 17

    Accepted Solution

    Since the computer isn't a member of the domain, the user logging on won't be a domain member and have the ability to add the computer to the domain without hard-coding a user account and password into the script, and using VB encoder to obfuscate it.  (There is a script decoder, so it only prevents curious folks from reading the account)

    Also, you can't rename a computer and join to a domain without a reboot between steps, which would kill the script...obviously.

    I've started it for you, the script below should rename the computer for you...hopefully this will move you forward.
    On Error Resume Next
    strCityCode = InputBox("Enter City Code: ", "Get City Code")
    strComputer = "."
    Set WSHNetwork = CreateObject("WScript.Network")
    strComputerName = WSHNetwork.ComputerName
    Set objShell = WScript.CreateObject ("")
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colBIOS = objWMIService.ExecQuery _
        ("Select * from Win32_BIOS")
    For each objBIOS in colBIOS
        strName = objBIOS.SerialNumber
    strNewName = strCityCode & "-LP-" & strName
    'WScript.Echo strNewName
    strReNameCommand = "netdom renamecomputer " & strComputerName & " /newname:" & strNewName & " /usero:administrator /passwordo:* /reboot:10"
    objShell.Run strRenameCommand

    Open in new window


    Assisted Solution

    I would recommend taking a look at the Microsoft Deployment Toolkit (Microsoft Deployment Toolkit 2010).  It is a free tool from Microsoft that will allow you to create an image to support your computers..  We have our installation setup by the default gateway of the site (multi-site company) so each site is assigned to connect to create the computer accounts in a separate OU.

    Author Closing Comment

    Ok great, thx

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
    New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now