Skip to main content

I am running a script that will ask the end user to enter the machine name. It will not work through Casper (remotely or in a policy) but will work fine if I run it locally. Any ideas why it may not be running? I have included the script below.



!/bin/bash



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



exit



Very odd the script runs fine when ran locally through the terminal. But stalls and never runs and never errors out when ran through a policy or through remote.



Thank you.

Not sure what you mean. You have to name the machine before you bind to AD, and renaming after that means local name and domain record are out of sync (which is why Windows doesn't allow that). So I guess my answer is yes, we name the machine and then bind it to AD, and that's all automated where possible using DNS as a setup requirement, and then the user logs in with their domain account and we use mobility settings for offline access later.


sorry I should have clarified. We have several machines that were "accidentally" bound to AD with names that don't follow our standards. I was wondering if anyone had success in changing machines names post AD bind and still preserving the domain account setup of the user.


I've not done that recently. And by recently I mean since OS 10.9 I think.... but as I understand it and remember it, it's as I said. The system name has no impact on the bind record name because the AD plugin is completely separate from the name in Sharing in every way. As such, changing the name would have no impact on the domain logins or cached accounts. In the other direction, if I'm not mistaken, you can script binding to happen to a record name other than defaulting to the machine name. I'd say grab a test machine to make sure before deploying something though :)


Thank you. Did not realize that Bind record and Machine name are not related. Changing machine name thru JAMF (post binding) causes failure when adding a new domain account to the system. Have to unbind and rebind to fix. Will test having separate names on our lab machine and update.


Hmmm.... I'm assuming if you send a rename through JAMF it's using the jamf setcomputername functionality which is doing at least 3 rename commands at the system level, possibly more. If you simply send a terminal command to set computername and localhostname (I think those are the two set in Sharing UI) I think you'll be fine (but again that means hostname and the AD record will differ so YMMV). Whatever JAMF is doing (again I assume you're just using the rename UI command in the console) it must also be changing the ID for the AD plugin which obviously breaks the bind. Yes it would be most clean to unbind and rebind with everything having the right name, but....
Good luck!


Yes thats what I am going to ask our team to test. See which breaks the bind functionality:
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName


One thing that popped up for me when running @rderewianko's script was to allow a JAMF process. I'm going to assume that's a KEXT of some sort that I can whitelist?


@rderewianko Using your script, I am getting the following:



Script result: 46:54: execution error: An error of type -10810 has occurred. (-10810)
The New Computer name is:
SCPreferencesSetHostName() failed: No such key
SCPreferencesSetLocalHostName() failed: No such key
Could not open prefs: No such key
Rename Successful






!/bin/bash



functions



function machinename () {
osascript <<EOT
tell application "Finder"
activate
set nameentry to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)
end tell
EOT
}



function renameComputer(){
#Set New Computer Name
echo "The New Computer name is: $ComputerName"
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName



echo Rename Successful
}



Script



ComputerName=$(machinename)
renameComputer
exit 0


@daniel_ross that is part of TCC, since its using a tell event it appears. same thing occurs with our similar script. We are trying to figure out a way to whitelist that but have not been able to yet.


I'm trying to accomplish the same thing, renaming a mac using Self Service. Has anyone had any luck with macOS 10.14/15? I used mm270's script (as well as others). When I select the policy in Self Service, it executes and hangs up on "Running", no other response.


Hi @mikedesmarais, @Mauricio gave me some good pointers here: https://www.jamf.com/jamf-nation/discussions/33691/self-service-get-user-input-with-script



Here's my hacked version of his script which I have in Self Service and looks to be working in Mojave and Catalina.
Note - I think I needed to create a Configuration Profile to give jamf greater access to avoid a PPPC popup.



#!/bin/bash
# GetUserInputFromSelfService-ComputerName.bash
# slightly modified from suggestion by Mauricio Pellizzon https://www.jamf.com/jamf-nation/discussions/32795/script-best-way-to-request-user-input
# 2019-10-29

userName=$(ls -la /dev/console | cut -d " " -f 4)

user_entry=""

validateResponce() {
case "$user_entry" in
"noinput" ) echo "empty input" & askInput ;;
"cancelled" ) echo "time out/cancelled" & exit 1 ;;
* ) echo "$user_entry" ;;
esac
}

askInput() {
user_entry=$(sudo -u "$userName" osascript <<EOF
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set theTextReturned to "nil"
tell application "System Events"
activate
try
set theResponse to display dialog "Please enter Computer Name" with title "Get Computer Name" buttons "Save" default button "Save" default answer ""
set theTextReturned to the text returned of theResponse
end try
if theTextReturned is "nil" then
return "cancelled"
else if theTextReturned is "" then
return "noinput"
else
return theTextReturned
end if
end tell
EOF
)
validateResponce "$user_entry"
}

askInput "$userName"

# Make it lower case - just a convention
lower="$(echo $user_entry | tr [:upper:] [:lower:])"

#/usr/local/bin/jamf setComputerName -name $user_entry
/usr/local/bin/jamf setComputerName -name $lower

# Update the server so it knows the name
/usr/local/bin/jamf recon

exit 0

I am having issues with my renaming convention. At the point of enrollment.



As we have soo many Non DEP machines, we are doing a Web Enrollment method.



So it will start with




  1. Change Machine Name @Enrollment trigger

  2. Base Apps @Enrollment trigger



So I used @rderewianko script it prompts but when I the second @enrollment trigger kicks in, towards the end it will bind to AD.
It binds to AD with the name as an example "Macbook Air" but the machine when I have checked it in system preferences etc it is what I entered when the dialogue box appears.



Any ideas what may have gone wrong


I was using this originally but it was so problematic to get the user dialogue box to appear.



!/bin/sh



###########################################################################


Change Computer Name



###########################################################################


hostname=$(/usr/bin/osascript <<-'EOF'
tell application "System Events"
activate
set input to display dialog "Enter New Computer Name: " default answer "" buttons {"OK"} default button 1
return text returned of input as string
end tell
EOF
)
echo "$hostname"



###########################################################################


Set New Computer Name



###########################################################################


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



sleep 20



exit 0 ## Success
exit 1 ## Failure


I pared this down so I can just type in what I want to name the computer. I run it as a Self Service policy only for Techs. My only practical use will be when we wipe devices and reassign them.



#!/bin/sh
# Name Your Computer.sh
#
#
# Created by Carey-Peterson, Rob on 10/25/19.
#

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your Computer name?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="$tag"

#set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon

@larry_barrett by any chance did this work as a policy, if you have tested this in that method?


Hi, We just had our jamf jumpstart this week, and this post piqued my interest. we use the machine serial numbers as part of the name which are preceded by our location codes. I have dug deep into scripting yet, but has anyone been able to accomplish something like this?


@k84 I didn't try, but when I try to run it via terminal (using sudo jamf policy) this is the message from the policy logs:



Executing Policy Name Computer
Running script Set Computer Name...
Script exit code: 0
Script result: 33:41: execution error: An error of type -10810 has occurred. (-10810)

There was an error.

You must specify a computer name. Use the -name flag


I never did get the dialogue box to enter a name.


See if this version works.



#!/bin/sh
# Name Your Computer.sh
#
#
# Created by Carey-Peterson, Rob on 10/25/19.
#


tag=$(/usr/bin/osascript << EOD
text returned of (display dialog "What is your Computer name?" default answer "" buttons {"Continue"} default button 1)
EOD
)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="$tag"

#set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon

@danlaw777 What is your hang-up? Trying to get the Serial Number or something else? here is how you can get the serial number in a script:



#!/bin/sh

serialNumber=$(system_profiler SPHardwareDataType | awk -F': ' '/Serial Number/ {print $2}')


After that, you can just append your location code.


@RBlount that worked when running it in terminal as a custom event.



Will try it again when the Mac gets placed into the JSS at the enrolment stage.
The script I used used previously worked but when my second enrolment trigger kicks in to install the base apps & then bind to AD for some odd reason binded the machine with a different Host Name although when checking system preferences > sharing the machine was what I entered when the dialogue box popped up.



Will let you know how I get on.


Hi @RBlount so when running it as a policy on first enrolment I get the this error



You must specify a computer name. Use the -name flag


When is the enrollment policy running? If the enrollment policy is kicking off while the setup or login screen is active, the user will never see the prompt to enter the computer name. if you can add the following to your script:



#!/bin/sh
set -x


Then we can see is happen in the script by looking at the policy logs.


Hi @RBlount, I'm trying to work with your script above and coming across a minor hiccup. If I run it as-is on Catalina from a .sh file invoked manually through Terminal, it works -- but when it gets to the step where it executes the jamf setcomputername process, it asks for a password within the Terminal window. My hope is to be able to kick this off via a policy, so the Terminal window won't be available. I've modified the script to invoke /usr/bin/scutil to do the renaming instead:



/usr/sbin/scutil --set ComputerName "${deviceName"
/usr/sbin/scutil --set LocalHostName "${deviceName"
/usr/sbin/scutil --set HostName "${deviceName"


...which, thankfully, brings up a GUI prompt for the user to enter a password. Problem is, it's prompting three times (once for each of those lines). Any ideas on how I can cut this down to one auth prompt (or get the jamf setcomputername command to issue its auth prompt in a window)?


Hi. I don't know if this helps at all (kind of a scripting novice) but this is what we use. It shows up in SS right after the machine set up is complete. We throw an "MBA-" in from of our machine names but it can be modified. Also it works on both 10.14 and 10.15.



!/bin/bash



Script to prompt for ComputerName, Set Name and Inventory in JAMF



assetTag=$(osascript -e 'text returned of (display dialog "Please type in the Asset Tag Number: "default answer "Enter Number Here" buttons {"Submit"} default button 1)')
computerName=MBA-$assetTag



Set Computer Name



scutil --set HostName $computerName
scutil --set LocalHostName $computerName
scutil --set ComputerName $computerName



Run Recon



jamf recon -assetTag $assetTag


Not sure if this adds any value but I have a policy that ties x3 separate scripts creating the computer name for my Organization.



The rename-computer.py script pulls from a .CSV that is located anywhere of your choosing. That file has a list of all your computers by Serial Number and what you want the computer name to be.



You can read all about it here: Automatically Renaming Computers from a Google Sheet with Jamf Pro



I have spoken with @haircut some time ago that stated he may or may not change the script to a .zsh in the future but no developments there. You can always subscribe to his GitHub and watch for any changes like I have. But as of right now this has been working flawlessly with every version of MacOS I have tested including 10.15.2