Posted on 08-09-2018 12:27 PM
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
Posted on 08-09-2018 12:34 PM
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.
Posted on 08-09-2018 12:46 PM
I think you will need to change the computer name with the jamf binary command instead.
jamf setComputerName -name "$ComputerName"
Posted on 08-09-2018 12:46 PM
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"
Posted on 08-09-2018 12:48 PM
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.
Posted on 08-09-2018 12:53 PM
ammonsc - I'll give that a go and see if it holds any better for us.
Posted on 10-02-2020 12:47 PM
@ammonsc I run the script locally and it works fine but it fails when deployed via policy or in SS any ideas?
Thanks!
Posted on 10-02-2020 01:07 PM
@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
Posted on 10-05-2020 05:57 AM
@RBlount,Thank you very much for the script it's working as expected.
Posted on 05-08-2021 02:37 PM
@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!