Running rename script on initial enrollment

jmltapp1971
New Contributor

Hey

Slowly setting up our new environment, hitting some walls with AD however not liking the name of the machine as it is too long to bind.

So I decided to make use of the script supplied by Freddie Cox at https://jamfnation.jamfsoftware.com/discussion.html?id=6034

The script is as follows

Set Hostname

read -p "Please Input Computer Name:" COMPUTERNAME
scutil --set HostName $COMPUTERNAME
scutil --set LocalHostName $COMPUTERNAME
scutil --set ComputerName $COMPUTERNAME

However it never seems to request the input of the computer name, and the log shows it failing due to the lack of the value named COMPUTERNAME.

I am just looking for a simple way initially for the user to be prompted on enrollment to enter a name so that it is set before I bind to the AD domain.

Any help anyone can give as to either what I am doing wrong, or changes I need to make to ensure the user is prompted before the script continues.

Many thanks

Jason

16 REPLIES 16

ShaunRMiller83
Contributor III

Jason,

Does the script work manually? I have found when troubleshooting if you put a set -x under the shebang it helps to see where the script is failing.

I am 100% speculating but I would bet the first command is actually working but it's not prompting with a GUI for user input but in the command line.

b0841f99006b4e20b4a26d3e0cdca14f

You could use something like CocoDialog to do what you want.

An Example of something similar I have done is:

#!/bin/sh

# Set CocoaDialog Location
CD="/Applications/utilities/CocoaDialog.app/Contents/MacOS/CocoaDialog"
serial="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*"(.*)"/1/')"
Model=`ioreg -c IOPlatformExpertDevice | grep MacBook | awk -F"= " '/model/{ print $2 }' | sed -e 's/^..//;s/..$//;s/[0,-9]*//g'`


# Dialog to enter the computer name and the create $COMPUTERNAME variable
rv=($($CD standard-inputbox --title "User Name" --no-newline --informative-text "Please enter the primary username for this Mac."))
USERNAME=${rv[1]}
rv=($($CD standard-inputbox --title "AD Binding" --no-newline --informative-text "Enter the name you want to BIND this machine to the domain with."))
ADCOMPUTERNAME=${rv[1]}

# Set Hostname using variable created above
scutil --set HostName $ADCOMPUTERNAME
scutil --set LocalHostName "$USERNAME"-"$Model"
scutil --set ComputerName "$USERNAME"-"$Model"

# Dialog to confirm that the hostname was changed and what it was changed to.
tb=`$CD ok-msgbox --text "Names Changed!" 
--informative-text "The computer name has been changed to "$USERNAME"-"$Model" and the AD bind name is set to $ADCOMPUTERNAME" 
--no-newline --float`
if [ "$tb" == "1" ]; then
echo "User said OK"
jamf policy -trigger ADBind
elif [ "$tb" == "2" ]; then
echo "Canceling"
exit
fi

mm2270
Legendary Contributor III

There are certainly better ways to do the renaming. @ShaunM9483 posted a good example of using available GUI tools to help make the input from a tech more accessible.

But just to help with your specific question, you can try this:

#!/bin/sh

function askForNewName ()
{

echo "Please Input Computer Name:"

read COMPUTERNAME

}

askForNewName

if [[ ${#COMPUTERNAME} -le 15 ]]; then
    echo "Valid name"

    scutil --set HostName $COMPUTERNAME
    scutil --set LocalHostName $COMPUTERNAME
    scutil --set ComputerName $COMPUTERNAME
    exit 0
else
    echo "The name "$COMPUTERNAME" is invalid. It it too long."
    sleep 1
    askForNewName
fi

It should verify that the name entered is within 15 characters long or it will prompt for a new name again.

I would also take a look at the jamf binary's commands for renaming a computer as it has some additional options that scutil doesn't have which may be useful to you.

jamf help setComputerName

brock_walters
Contributor
Contributor

Just thought I'd throw my 2¢ in here...

ShaunM9483 is correct. If you only ran the script as you posted it your users would have to input their information into a Terminal session which is probably not the behavior you want.

Below is a very simple script that contains an AppleScript that will pop up a field for your users to input their Computer Name. Also to avoid the prompt for an administrator password when calling scutil this script should always be run by the root user, i.e, sudo sh nameofscript.sh or as a payload in a JSS policy. This little bit of AppleScript has many other uses too! Good luck!

#!/bin/bash

# check that script is run as root user

if [ "$EUID" -ne 0 ]
then
    >&2 /bin/echo $'
This script must be run as the root user!
'
    exit
fi

# capture user input name

while true
do
name=$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter a name for your computer or select Cancel." default answer ""' -e 'text returned of result' 2>/dev/null)
    if [ "$?" -ne 0 ]     
    then # user cancel
        exit
    elif [ -z "$name" ]
    then # loop until input or cancel
        /usr/bin/osascript -e 'Tell application "System Events" to display alert "Please enter a name or select Cancel... Thanks!" as warning'
    elif [ -n "$name" ] # user input
    then    
        break
    fi
done

scutil --set ComputerName "$name"

tcandela
Valued Contributor II

@brock_waltersthe script works great but can you possibly edit this osascript you created so it doesn't have the resulting popup "jamf wants access to control system events"?

20220216_130325.jpg

jmltapp1971
New Contributor

Thank you for your suggestions guys im going to try both approaches out and see which works best for us. I did get some success yesterday with getting a dialog box up but even though the commands showed successful i did notice that the names of the machines did not change except on the sharing screen - the casper names for example stayed the same despite it.

Sorry very new to the world of Macs, and this is definately becoming a re-education after years of PC support.

jmltapp1971
New Contributor

Thank you for all of your help guys. In the end Brocks script with some minor changes got me the solution each and everytime. This coupled with a jamf recon command updated all the JSS records straight away to match.

My next step is to try and automate this a little more so the user only has to type their site code and date of purchase with the partial mac id we use being automated, but that is for another day - Again thank you all.
Jason

jason_bracy
Contributor III

Just as an alternative, we have a script that automatically sets the computer name to the serial number - with a "-M" suffix. This way we know that the name will be unique in both the JSS and in AD, and in AD we can quickly find all of the Macs (setting the NTP server also ensures that the AD bind doesn't fail due to the clock offset):

#!/bin/bash
systemsetup -setnetworktimeserver ntp.yourad.com
serial=`ioreg -l | grep IOPlatformSerialNumber | awk '{print $4}' | 
cut -d " -f 2` 
computername="$serial-M"
scutil --set LocalHostName "$computername"
scutil --set ComputerName "$computername"
scutil --set HostName "$computername"

mm2270
Legendary Contributor III

@jason.bracy Its easier to use the jamf binary for your computer naming.

jamf setComputerName -target / -useSerialNumber -suffix "-M"

jason_bracy
Contributor III

@mm2270 Never even looked for that - since we were using that script prior to using Casper, I never even thought to change it :-)

Thanks for the pointer.

jmltapp1971
New Contributor

Thanks guys

Unfortunately what my managers want is the name to match our PC naming convention.

So this would be

Site Code (4 characters) Machine Type (1 Character - A for all Apples) MM/YY (Purchase date) Last 5 of the mac address

So with my machine it is MKEEA1506F6021

At the moment it is a manual process, but would like to get to the point that at least the mac ID is pulled to make it easier on the users.

jason_bracy
Contributor III

You could use the osascript to pull in the Site and Date variables one at a time, so all the user needs to do is select the right site code and date, and have scutil get the last 5 of the MAC address (though which MAC address is the real question), then combine them to make the computer name...

Don't have time now to play around, but there are plenty of resources on how to have osascript present a list to select from...

dpertschi
Valued Contributor

@mm2270 jamf setComputerName, does that also set the Host and Local Host names?

thundercr250
New Contributor II

I realize this is an old post but...

I am successfully running the rename computer script as a policy.

Is it possible to have it run during the setup assistant or at the login screen?

The issue I face is with our naming convention. I need it to rename the Mac and then bind to AD.

Currently it does this correctly but only after I log into a local account created be prestage enrollment.

I need it to prestage enroll, rename the Mac, then join to AD. If it doe not rename and bind to AD the user will not be able to login with AD credentials but the script doesn't seem to run until a user is logged in.

The idea is for this to be an automated process. I cannot give out the local account password to users so that it will finish enrollment policies.

cbrewer
Valued Contributor II

@thundercr250 I have a bash script that uses AppleScript to prompt for a computer name during Setup Assistant. You have to run this script with a policy set for the enrollment trigger and then leave your Mac at the Time Zone setup screen until the name prompt pops up.

After you capture the name, you could add to the script to bind to AD, etc.

https://github.com/cwmcbrewster/Jamf_Scripts/blob/master/Computer_Enrollment_PromptForName.sh

djwojo
Contributor

@cbrewer Thank you SO MUCH for sharing this. I got a huge jumpstart (pun intended) on the year of 0-touch. :)

mhegge
Contributor III

Using the following slick method:

https://www.macblog.org/post/automatically-renaming-computers-from-a-google-sheet-with-jamf-pro/