Workstation Naming Conventions for Active Directory

Been a long time since I posted anything so I thought I’d throw out a handy trick I’ve come across in a few places to make life easier for deploying workstations. Most of the places I’ve worked in have been Dell shops, though this may work pretty well with other brands of workstations.

You want to automate as much as possible when setting up stuff in large deployments and joining computers to the domain is an annoying task made easier with a VB script. One of the really annoying things is naming the computer. So naming them something that is ‘hard coded’ into the BIOS and printed on the computer can make life a lot easier. Usually this is a serial number. In the case of Dell this is the Service Tag.

There are a ton of utilities out there that will retrieve the Dell service tag from the BIOS as the “Serial Number” and it’s a function built into the Windows API. Since you use this service tag for inventory, warranty work and everything else already it makes sense to use it as the host name.

Automating Using the Dell Service Tag as The Host Name of the PC

Here is a script I found and have tweaked a bit for various networks I’ve been working with over the last few years. This one gets the Dell service code from the BIOS after joining the computer to the domain as its current name then renames it. Yeah it’s a bit sloppy but sometimes your images aren’t super clean and you need to be using domain credentials to do this. This script is definitely not mine, I got the original script here. Looks like he might have a more refined version now. The code for getting the serial is from a VB message board, some of it is mine too. If you made that part, let me know so I can credit you.

 

On Error Resume Next
‘ This script joins the current computer to a domain, using specified user and placing it in specified OU
‘ Created by Sole Viktor – sole@sole.dk
‘ Added getting serial number code from BIOS -Aaron

‘ Show Status
‘ Set theese variables
strDomain = “Enter Your Domain Here” ‘ Domain to logon
strPassword = “ENTERPASSWORDHERE” ‘ Service account logon password
strUser = “Enter Your Domain Admin UserName Here” ‘ Service account
strOU = “OU=Computers,DC=Local,DC=Domain,DC=com” ‘ OU to place computer in

‘ Constants to choose from when joining
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144

Set objNetwork = CreateObject(“WScript.Network”)
strComputer = objNetwork.ComputerName
‘ Join Domain
Set objComputer = GetObject(“winmgmts:{impersonationLevel=Impersonate}!\\” & _
strComputer & “\root\cimv2:Win32_ComputerSystem.Name='” & _
strComputer & “‘”)
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
strPassword, strDomain & “\” & strUser, strOU, _
JOIN_DOMAIN + ACCT_CREATE + DOMAIN_JOIN_IF_JOINED)

Set objWMIService = GetObject(“winmgmts:” & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colSMBIOS = objWMIService.ExecQuery (“Select * from Win32_SystemEnclosure”)
For Each objSMBIOS in colSMBIOS
newComputerName = objSMBIOS.SerialNumber
Next
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:” _
& “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”)

Set colComputers = objWMIService.ExecQuery _
(“Select * from Win32_ComputerSystem”)
For Each objComputer in colComputers
MsgBox “About to rename computer to: ” & newComputername
ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)
If ErrCode = 0 Then
MsgBox “Computer renamed correctly.”
Else
MsgBox “Eror changing computer name. Error code: ” & ErrCode
End If
Next

Select Case ReturnValue

Case 0 Status = “Success”

Case 2 Status = “Missing OU”

Case 5 Status = “Access denied”

To use the script just copy and paste the quoted text into a text file, edit the proper variables at the top and save it. I won’t give a step by step guide here because I’ve had to adjust this a lot to every network I’ve been on and this specific script MAY not work exactly like it’s written for your network. I’ve always had to adjust it a little. For instance, you can move the code that gets the serial number before the join domain code in some places and not others depending on what OS you’re deploying. Why? I have no idea, just depends on the Windows Image you made.

  • Typically I’ll place this script in a folder called C:\Scripts on my image when I’m making it and name the script itself “JoinDomain.vbs”.
  • In Windows 7 the way you have to run this one is right-click on Powershell and hit “Run as Administrator” then navigate to the Scripts directory and type ./JoinDomain
  • If it works right you’ll see a bunch of Message boxes that say each step was a success.
  • If you’re using this to reimage a computer you’ll need to delete the computer from AD first. You could probably add code to the script to do this for you, I’ve never done it.
  • If you’re using Spiceworks, Desktop Authority or some other desktop management software the Last Login fields and inventory management is crucial to figuring out which user is on which computer.

 

Leave a Reply