Renaming Computer Issues

arepko
New Contributor III

I am using a simple AppleScript here to rename our computers before they get bound to AD. The computer name changes via the script, but as soon as it "Checks in" the names change back to MacBook Air. Occasionally it works fine and does not change back, but there is no rhyme or reason as to why. I have verified that NONE of my policies have the "Reset Computer Names " under the Maintenance Policy checked.

These are brand new machines never before logged in.

#!/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
/usr/sbin/scutil --set ComputerName "$ComputerName"
/usr/sbin/scutil --set LocalHostName "$ComputerName"
/usr/sbin/scutil --set HostName "$ComputerName"
echo "Rename Successful"
dscacheutil -flushcache
jamf recon
exit 0
9 REPLIES 9

ShaunRMiller83
Contributor III

I know you mentioned you checked your policies but do you happen to have a Login Window Config Profile?

In that Config Profile payload is an option to "Set Computer Name to Computer Record Name"

Also have a close look at your other Config Profiles as well. I have been bit a few times where JAMF deploys a config profile with a "hidden" unrelated setting and causes some havoc.

ammonsc
Contributor II

I think you will need to change the computer name with the jamf binary command instead.

jamf setComputerName -name "$ComputerName"

ammonsc
Contributor II

We use the following script

#!/bin/bash

NewComputerName="$(osascript -e 'Tell application "System Events" to display dialog "Enter the Asset ID Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"

jamf setComputerName -name "$NewComputerName"

jamf recon -assetTag "$NewComputerName"

arepko
New Contributor III

We do have a Login Window Config Profile! Thought for sure it would be this! But... verified that it is Not checked as well.

Verified the rest of our Config profiles as well. None of them have any settings for the name.

Going to RE-double check all of the policies again.

arepko
New Contributor III

ammonsc - I'll give that a go and see if it holds any better for us.

csanche3x
New Contributor II

@ammonsc I run the script locally and it works fine but it fails when deployed via policy or in SS any ideas?

Thanks!

Tribruin
Valued Contributor II

@csanche3x , when running from a policy, the script is running as root, not as the local user, so the prompt is never shown to the use. Try this revised script:

#!/bin/bash

currentUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

NewComputerName="$(sudo -u $currentUser osascript -e 'Tell application "System Events" to display dialog "Enter the Asset ID Number:" default answer ""' -e 'text returned of result' 2>/dev/null)"

jamf setComputerName -name "$NewComputerName"

jamf recon -assetTag "$NewComputerName"

For more information, take a look at the JNUC presentation "Scripting Jamf: Best Practices" by Armin Briegel (scriptingosx). Here is a link his the resources page on his website ScriptingOSX-JNUC2020

csanche3x
New Contributor II

@RBlount,Thank you very much for the script it's working as expected.

etarasula
New Contributor II

@RBlount

I put this script into Self Service and it seems to run just fine but there is nothing happening on the screen. Any idea what gives?

Never mind. Evidently it take a little bit!