Get and Rename Computer Name Interactively

johnklimeck
Contributor II

Guys,

I am looking for something similar. I already have a rather basic script that returns, ComputerName, LocalHostName, and HostName.

Now, I want to add to the script, with interactive function.

Enter the "new" ComputerName (which will then also be used for LocalHostName and HostName), gets set by scutil --set, then return / list the new names for all three.

thanks,

jk

#!/bin/bash

function GREEN {
tput setaf 2
}

function ENDColor {
tput op
}

echo ComputerName

GREEN
scutil --get ComputerName ENDColor
echo LocalHostName

GREEN
scutil --get LocalHostName
ENDColor

echo HostName

GREEN
scutil --get HostName
ENDColor

echo
tput setaf 5
echo "Make sure the names all match - if they don't, tell a grown up near you!"
tput op

echo
echo
echo "press Enter to close this window"
read

9 REPLIES 9

freddie_cox
Contributor III

Not sure if this is what you are wanting or not, but this what I use for an interactive script by our Techs to set the computer name before our AD Join:

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

I've also got a policy that brings up a dialog box with AppleScript so it can be run in the background, asks for input then disappears:

#!/bin/bash
#Freddie Cox for Knox County Schools
#Edited by Justin Ellis
#2012

ComputerName=`/usr/bin/osascript <<EOT
tell application "System Events"
    activate
    set ComputerName to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)
end tell
EOT`

#Set New Computer Name
echo $ComputerName
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName

echo Rename Successful

I don't do anything with colors so you could adjust that to your needs. I'm sure some checking could also be built in, but I've had pretty good luck at this point to where I haven't worried about it.

This was so useful and with some tweaking I got it to set based on the user's email. I also took the Apple Script out of the tell which eliminates a security prompt for access. I then went ahead and made the OSA script into a function for future use. 

 

#!/bin/bash
#Freddie Cox for Knox County Schools
#Edited by Justin Ellis
#2012

#Edited for PandaDoc by MICHAEL ZIMMERMANN
#Edit 07.14.21 mmddyy

####Function displays dialog box with question $1, default answer $2 and title $3
####$1-3 are passed from the function call
function prompt() {
  osascript <<-EOS
      text returned of (display dialog "$1" default answer "$2" buttons {"OK"} default button 1 with title "$3")
EOS
}
####sets results of function email to variable email
####prompt '$1' '$2' '$3'
email=$(prompt '$1' '$2' '$3')

echo $email
####Sets var computer name to everything before "@"
computerName=${email%@*}
####Sets computer, host,and local host names to var computername
echo $computerName
scutil --set HostName $computerName
scutil --set LocalHostName $computerName
scutil --set ComputerName $computerName

echo Rename Successful

johnklimeck
Contributor II

Freddie,

Just checked this, since I now have a need for just this thing. Both the bash and osascript / Applescript are awesome. Working perfectly.

Thanks,

John K

pueo
Contributor

Hi Folks

Just wondering if any of you gotten a renaming Script to rename with out the mac being logged in.
The only way we have it work is - DEP finishes initial setup -> login with Hidden Admin account-> Policies run in background-> AppleScript runs and Rename box pops up -> Enter Machine Name, hit submit (this updates Computer,Local and HostNames) -> Device is joined to AD.

I want to achieve the same results WITHOUT logging into the device first.

Thanks everyone.
Ashley

JJL
New Contributor

I'm right there with you Ashley. We've been trying several combinations of this across the forms, currently, the only "working" version we have is either the sheets one where you enter the name ahead of time into a google sheet. Or we got a DEP pop-up to come up but only after logging in and that one happens after AD Bind, so we have to unbind and rebind for it to be proper in our environment. If I come across the proper config to get a prompt pre-login, I'll post back here.

Our dream is to have the first script run after we accept JAMF remote management, that script prompts for the computer name to be typed in, and delays until the name is entered, then renames the machine to the input. Then allows the rest of the enrollment / other scripts to run. All before first login happens.

tsmith182
New Contributor II

@pueo The other couple options I have been exploring is using an api call to an internal resource to grab computer name based on S/N, the other option is if you can pre register the mac on your network and use the Bash command 'hostname - s'. Hostname does a reverse DNS lookup and the '-s' switch will cut the full qualified portion. Thus then saving that output to a variable and setting the computer name. example: companyname-123456.companyname.com '-s' will leave you with compayname-123456, save that to variable and then run your scutil commands to set the new computer name.

If you pre-register the mac on your network, have your post enrollment policy (with the script) triggered on DEP enrollment completion. Something to try and or think about.

#!/bin/sh
compName=$(/bin/hostname -s)
scutil --set HostName $compName
scutil --set LocalHostName $compName
scutil --set ComputerName $compName

janthenat
New Contributor II

How does one get a script (interactive or not) to run before the domain bind configuration profile which is part of our PreStage Enrollment?

jhuls
Contributor III

@janthenat I have a script that looks up the machine serial number in a csv that it pulls from a local file share and then renames the machine based off of that. Once it's renamed it then binds the machine and places the object in a specific OU based on certain criteria in our naming scheme. This script is ran via a policy that I originally tried to use with the enrollment trigger but that failed more often than not so it moved to Self Service. Recently I implemented depnotify which makes a call to that policy among others in the order I want. It too struggles with the enrollment trigger so it's setting in Self Service as one icon to do the entire build until I can implement a way to get depnotify to be more reliable at triggering.

In case you're wondering about the csv this is created from our inventory database system every 30 min.

So far it's working well except for a couple things. It breaks if there's already an object in AD with the same name in the same place. This happens on occasion unfortunately. The script is crude and sloppy so it doesn't have anything in place to deal with that and if this issue occurs and is left unattended, the drive literally can run out of disk space. One of these days this will get fixed but we make it work for now. Also, now that we have users offsite due to covid, the process doesn't work well if the user needs to wipe their system due to issues or gets a new computer because of the need for access to that local fileshare. We could probably make it so that vpn is needed to complete the build but that makes it sort of sludgy to some extent. I'd like to investigate wrapping the csv in a dmg on a schedule and automatically importing into Jamf to then deploy before the script runs. Time isn't on my side right now though. At this rate I might not get to that until next year so we make the current setup work. I should add that the idea of this last part is to not bind either.

csanche3x
New Contributor II

Hello all, does anybody have a version of this script that works on Catalina?

Thanks!