Naming Scrip occasionally failing

jsexton
New Contributor

Hey All! I have been working on a section of my provisioning script that names the computer then I'm using the built-in Jamf binding tool after the script runs so the proper name is used. The problem I'm running into is that from time to time the rename won't stick and it'll upload as [localadmin]'s Macbook Pro. Anyone else experienced this?

! /bin/sh

Prompt user to enter the machine name

MachineName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Enter the Machine Name:" default answer "" with title "Rename Machine" with text buttons {"Ok"} default button 1' -e 'text returned of result')"

Set the ComputerName

sudo scutil --set ComputerName $MachineName

sleep 15

Set the LocalHostName

sudo scutil --set LocalHostName $MachineName

sleep 15

Set the HostName

sudo scutil --set HostName $MachineName

sleep 30

exit 0

2 REPLIES 2

mroe
New Contributor III

Where do you have it set to trigger, and are you flushing your policies if it's happening to previously enrolled devices?

The way I have our work flow set up for example is, after prestage enrollment is complete trigger this script

#!/bin/bash


LoggedInUser=$(stat -f%Su /dev/console)
LoggedInUID=$(id -u "$LoggedInUser")

computerName=$(/bin/launchctl asuser "$LoggedInUID" sudo -iu "$LoggedInUser" /usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter computer name and click Submit:" default answer "" buttons {"Submit"} with icon caution' -e 'text returned of result' 2>/dev/null)

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

dscacheutil -flushcache

echo "Computer name has been set.."
echo "<result>' scutil --get ComputerName'</result>"

I also have a profile to allow the JAMF agent to act as a privileged user without user interaction: https://github.com/jamf/JamfPrivacyPreferencePolicyControlProfiles

Together this makes a dialog popup after the prestage enrollment screen (the one with the cogs) after about 10 seconds of moving onto the privacy and data section. If it's a previously enrolled device I clear all the policies before starting enrollment.

Edit: Also take note that running sudo in a script changes the environment for the script to root(/) which is probably also a cause of your script failing. Using the PPPC profile from JAMF I linked above will remove the need to sudo to make system changes with scripts as the jamf agent.

rnoureddine
New Contributor III

I'm seeing a similar behavior,
Here is the script that I'm using

!/bin/sh

Prompts for user input to name computer. Used for JAMF enrollments.

Gather user

loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")

Capture user input

computerName=$(/bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" /usr/bin/osascript -e 'tell application "System Events" to set computerName to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)')

Rename

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

**But recently I have been seeing this error:
Actions from policy log: Executing Policy Enrollment - 75 - Computer Name Running script enrollment-computerName.sh... Script exit code: 1 Script result: 74:151: execution error: An error of type -10810 has occurred. (-10810)
Could not open prefs: No such key
SCPreferencesSetHostName() failed: No such key
SCPreferencesSetLocalHostName() failed: No such key

Error running script: return code was 1.