Change ComputerName/LocalHostName/HostName to match AD Bind Name

coreyradford
New Contributor

For whatever reason, the ComputerName on all of our macbook airs on campus have been changed to "Macbook Air". Obviously, this has been irritating to the end users and even more irritating to IT.

I'm thinking there may be a way to use dscl to read the name of the computer from AD and then pipe that to a scutil --set ComputerName command.

Does anyone have a good way to do this?

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

As long as the local AD bind data still has the correct name, you can get that from the Mac itself with:

dsconfigad -show | awk '/Computer Account/{print $NF}' | sed 's/$$//'

The name returned may be in lower case, or upper case. Depends on your environment. If you want, you can convert it to all upper case, like so:

dsconfigad -show | awk '/Computer Account/{print toupper($NF)}' | sed 's/$$//'

or vice versa if you want them all lowercase, change toupper to tolower

Then send that to your naming command.

As as aside, I would look into why they have had their names change. That typically does not happen, so something caused it.

View solution in original post

6 REPLIES 6

Chuey
Contributor III

So when you do a jamf getComputerName it's returning "Macbook air" ?

mm2270
Legendary Contributor III

As long as the local AD bind data still has the correct name, you can get that from the Mac itself with:

dsconfigad -show | awk '/Computer Account/{print $NF}' | sed 's/$$//'

The name returned may be in lower case, or upper case. Depends on your environment. If you want, you can convert it to all upper case, like so:

dsconfigad -show | awk '/Computer Account/{print toupper($NF)}' | sed 's/$$//'

or vice versa if you want them all lowercase, change toupper to tolower

Then send that to your naming command.

As as aside, I would look into why they have had their names change. That typically does not happen, so something caused it.

stevewood
Honored Contributor II
Honored Contributor II

If your computer names have spaces, like if you use a person's name for the computer name, the command @mm2270 gave you may not return everything. I tried it on mine and it only returned the second half of the name (wood in my case). This should return the entire name:

dsconfigad -show | grep "Computer Account" | awk {'print $4 " " $5'} | sed s'/.$//'

coreyradford
New Contributor

@mm2270 yeah, we have looked into it and it is anyone's guess. In the logs it shows the name change, but doesn't show the action that triggered it. So far, working with JAMF support and searching through jamfnation we have narrowed it down to 1) documented bug in 10.10.2 or 2) database ghost records / corruption or 3) conflicting configuration profiles.

None of these have yielded any hard evidence, but as the issues seemed to have only lasted about a week, we are thinking we can begin renaming computers now.

coreyradford
New Contributor

@Chuey well, yes. Or macbook air (8) or macbook air (30) ...you get the idea.

TomH
New Contributor III

We use the FQDN and remove any spaces:

#! /bin/bash

###
# Computer name cleanup - v0.1 - Thomas Holbrook
###

shopt -s nocasematch

cutfqdn=`sudo /usr/libexec/PlistBuddy -c "print :ActiveDirectory:fully-qualified domain name:" /Library/Preferences/OpenDirectory/DynamicData/Active Directory/JIGSAWSYSTEMS.plist | head -n1 | cut -d "." -f1`

echo "AD FQDN is $cutfqdn"

cutfqdnNoWS="${cutfqdn//[[:space:]]/}"

echo "Clean FQDN is $cutfqdnNoWS"

currentComputerName=`sudo scutil --get ComputerName`
echo "Current computer name is $currentComputerName"

currentLocalHostName=`sudo scutil --get LocalHostName`
echo "Current local host name is $currentLocalHostName"

currentHostName=`sudo scutil --get HostName`
echo "Current host name is $currentLocalHostName"

currentBashHN=`hostname`
echo "Current host name is $currentBashHN"

if [[ $cutfqdn != "macbook air"* ]] && [[ $cutfqdn != "" ]]; 

    then

        if [ "$currentComputerName" = "$cutfqdn" ]; then

          echo "Current computer name is OK"

          else 

          echo "Incorrect Current computer name updateing"

          sudo scutil --set ComputerName $cutfqdn

        fi

        if [ "$currentLocalHostName" = "$cutfqdn" ]; then

          echo "Current local host name name is OK"

          else 

          echo "Incorrect Current local host name updateing"

          sudo scutil --set LocalHostName $cutfqdn

        fi

        if [ "$currentHostName" = "$cutfqdn" ]; then

          echo "Current host name name is OK"

          else 

          echo "Incorrect Current  host name updateing"

          sudo scutil --set HostName $cutfqdn

        fi

        if [ "$currentBashHN" = "$cutfqdn" ]; then

          echo "Current host name name is OK"

          else 

          echo "Incorrect Current  host name updateing"

          sudo hostname $cutfqdn

        fi

        exit 0

    else

        echo "Are we bound to AD? / Not Bound as macbook air?"

        exit 1

fi