Appending Existing Computer Names

cghigliotty
New Contributor

The naming convention on our Mac platform is firstname.lastname followed by OS version. Example:

John.DoeXVI

I'd like to append only the beginning of each computer name to reflect different AD OU's. For example:

ADOU1-John.DoeXVI
ADOU2-Jane.DoeXVI

I know you can go into the JSS, manually change the computer name, and then reset it using Casper Remote, but I was hoping to implement a script that could add the appropriate prefix based on an AD Security Group or AD Computer ID in Casper. So, for example if the AD Computer ID was ADOU1-13453, it would know to apply ADOU1 to the Mac's computer name.

I have to do this for ~500 computers, and so I'm hoping I can automate this as much as possible.

Anyone have any ideas or scripts that could be useful?

Thanks guys!

Does anyone know if this is possible?

4 REPLIES 4

mm2270
Legendary Contributor III

It should be possible to do what you're looking at, but its also a little unclear on exactly what criteria you want to use for the rename. At first you mention AD OU, but later you refer to AD Security Group or AD Computer ID. These aren't usually the same thing, so you'll have to decide which one you want.

I assume since you're talking things like AD OUs and such that all the Macs are joined to your Active Directory.
If you wanted to use something like the AD computer name as the item to append it would be pretty easy, however, in most cases that information matches the local computer name + $ sign at the end. So for example, the Computer Account in AD for your "John.DoeXVI" computer is likely "John.DoeXVI$" or "john.doexvi$" unless you're using some method that binds them all under a completely different name to AD.

In any event, a script that ran once against the Macs as they did their regular check-in to rename them according to both local and directory stored information should definitely be possible.
If you can clarify what piece of information you really want to use, we can help you come up with a working example.

cghigliotty
New Contributor

The AD computer name is different from the local computer name. The AD computer naming convention is city + ADOU + asset number. So for example:

NYCADOU1-12345
NYCADOU2-25677

As mentioned before, the local computer name is firstname.lastname followed by OS version and phone extension (sorry, I didn't mention the extension in my initial post. For example:

John.DoeXVI2020

The reason the two are different is because it's nice to have the end user name and extension easily available for contact; it's also easier search for a name rather than a naming convention in Casper Remote.

I wasn't sure which criteria would be easier to use, so I included both. The AD computer name would be the easiest. So essentially, if John.DoeXVI2020 has an AD computer name of NYCADOU1-12345, I would like the script to take the city and Active Directory OU—in the example, NYCADOU1—and append it to the local computer name. The result would hopefully be:

NYCADOU1-John.DoeXVI2020

Thank you for your help!

mm2270
Legendary Contributor III

Well, if you're good with using the AD computer name, this should be a simple thing.

From Terminal on any of your Macs joined to AD, run:

dsconfigad -show

You should see a line in there called Computer Account which lists the AD Computer name, for example, "NYCADOU1-12345" from your example above.

Using this information, a script could pull both the AD name and the local Computer (Sharing) name and extract just the elements you want, combining them into a new name variable used in a rename command.

Here's a simple example.

#!/bin/sh

LocalMacName=$( scutil --get ComputerName )
ADMacName=$( dsconfigad -show | awk '/Computer Account/{ print $NF }' | cut -d- -f1 )
NewMacName="$ADMacName-$LocalMacName"

echo "New Mac name will be: $NewMacName"

scutil --set ComputerName "$NewMacName"

echo "Mac name was set to $( scutil --get ComputerName )"

Since I obviously don't have access to your specific environment, you'll need to test the above carefully. Quick explanation on what's happening above.

LocalMacName=$( scutil --get ComputerName ) <-- This pulls the existing computer sharing name into a variable to be used later. So it should pull something like "John.DoeXVI2020"

ADMacName=$( dsconfigad -show | awk '/Computer Account/{ print $NF }' | cut -d- -f1 ) <-- This pulls the AD computer name from the Mac itself using dsconfigad, but I'm using cut to cut the first part of the name before the hyphen as the stored information, so in the example you provided, it would give me "NYCADOU1"

NewMacName="$ADMacName-$LocalMacName" <-- This takes the 2 above variables and combines them into one as a new stored string, so it should be something like "NYCADOU1-John.DoeXVI2020"

echo "New Mac name will be: $NewMacName" <-- Just an echo command to make sure we're getting what we expect. When running this in testing mode, I suggest you comment out the next 2 lines just to echo back what the new name will be without doing the actual rename.

scutil --set ComputerName "$NewMacName" <-- Uses scutil to rename the Mac's sharing name to the new one

echo "Mac name was set to $( scutil --get ComputerName )" <-- Just echoes back the new name so we can confirm the OS sees its new sharing name.

Give it a try and if you have trouble, post back. Again, test it thoroughly before trying to run it against 500 Macs :)

cghigliotty
New Contributor

The script has worked great so far! Pushed it out to a small test group of about thirty computers, and it was flawless. I've started widening the scope and it's appended roughly 200 computers successfully.

Thank you so much!