Script to Name Computers


My team has nobody with any scripting ability, so this is tough for us because it takes a lot of the power of Casper Suite out of our hands.

When we currently manually image a machine, we name our machine under a "Dept-Building-Asset Tag" naming scheme.

Say we have departments A-M and buildings AA-LL. Could somebody explain (or point me to a resource that would teach me) how to use pre-stage enrollment with smart groups to change the computer name during enrollment to follow our current naming convention? Each asset tag would need to be added manually, if that adds some complexity to this issue.

EDIT: I should've been more clear. There are a couple things we want/need out of this script. The first, and this is a need, is to have this script run prior to binding to AD. The second is that our "Building-Dept-Tag" format has obvious abbreviations due to the 15 character limit. Is there a way to write a script where it goes something like "If Building = XX, display ABCD, if department = YY, display abcd, insert random four digits for tag"?


Legendary Contributor III

Where does the Asset tag come from, or, how is it determined per Mac? Is it just a random or running number? Or something that comes from an internal resource?
What about Dept and Building? Are these coming from AD or some other authoritative resource?

In short, I think to help you create a script we need to know a little more about where all this information is coming from, if any.

Valued Contributor II

I suppose I would write a basic applescript to 'display dialog' to prompt for the information, then 'do shell script' to execute scutil (or jamf setcomputername) and set the computer's name. Barring that, you could just go a little more creative...

ComputerName=`perl -e 'open IN, "</usr/share/dict/words";rand($.) < 1 && ($n=$_) while <IN>;print $n'`
AssetTag=`od -vAn -N4 -tu < /dev/urandom`

scutil --set ComputerName "$ComputerName-$AssetTag"
scutil --set LocalHostName "$ComputerName-$AssetTag"
scutil --set HostName "$ComputerName-$AssetTag"


@mm2270 So, our organization integrates with Oracle (exactly how/what/etc I am completely unsure of) to determine department and building location. I am able to get the correct department from AD, but the building does not show.

Asset tags are taken from a physical sticker placed on each machine. Each tech (Windows or Mac) has a sheet with a range of stickers that are affixed to each machine and become the "asset tag" portion of the computer name. This means that, as of currently, I could image a machine and assign an asset tag of 3443, and my teammate could image the next machine and it could have a tag of 1089, because thats the next number on the sheet of stickers.

Legendary Contributor III

OK, so, given that 2 of the 3 pieces of info you need to use to create the computer name are not things you can grab automatically from a script, I'd say you would need to go the route that @thoule suggested (first suggestion) and create an app that would ask for authentication from a tech, then ask for input of the different pieces/strings, like Department, then Building, then Asset Tag. It can combine all these together into a computer name, then rename the computer appropriately.
I don't really know the details of how your environment is set up, but it might be prudent to do an ldapsearch check against AD to make sure the name its chosen isn't already in use before it attempts to bind the Mac to AD. You didn't mention AD binding, but I'm assuming that's being done somewhere in this process, correct?

Valued Contributor II

I felt a little guilty being a wiseguy so here's something to get you started. You'd need to save this as an App in Script Editor, maybe package it up and install it via Casper into /Applications/Utilities/ and then open it. You should be able to use 'open /Applications/Utilities/' within a policy; or just have the test open it.

set scrResult to (display dialog "Enter the Department name" default answer "")
set dept to text returned of scrResult

set scrResult2 to (display dialog "Enter the Building name" default answer "")
set build to text returned of scrResult2

set scrResult3 to (display dialog "Enter the Asset Tag" default answer "")
set tag to text returned of scrResult3

set newName to dept & "-" & build & "-" & tag

do shell script "scutil --set ComputerName " & newName with administrator privileges
do shell script "scutil --set LocalHost " & newName with administrator privileges
do shell script "scutil --set HostName " & newName with administrator privileges


You can try this one.



Thanks all for the help, but I realized I was nowhere near as clear. Now that 9.91 allows for very clear binding during Setup, it keeps creating devices with names "Admin's MacBook", which isn't great for our AD. If anybody can give some clarity, it would be appreciated.

Valued Contributor


Is it possible to use one of the above scripts, making the last step to enroll via command line?