Posted on 07-19-2018 11:03 AM
Hello,
We need to have a dialog prompt so users can name the machines Pre-Jamf enrollment. This script (written as a .COMMAND shell file) seems to work but it'll ask for the current user password, then machine name both in dialogs but then still asks for a password in terminal prior to running. Any ideas?
#!/bin/sh
CurrentUser=$(ls -l /dev/console | cut -d " " -f 4)
currentUserPassword=$(sudo -u $currentUser /usr/bin/osascript <<END
tell application "System Events"
activate
set dialog to (display dialog "Please enter the password for your current account " & "(" & "$currentUser" & ")" & ":" default answer "" with title "$orgName" buttons {"Continue"} default button 1 giving up after 120 with hidden answer)
set answer to text returned of dialog
end tell
return answer
END
)
HOSTNAME=osascript -e 'set T to text returned of (display dialog "Machine Name?" buttons {"Cancel", "OK"} default button "OK" default answer "")'
sudo scutil --set ComputerName $HOSTNAME
sudo scutil --set HostName $HOSTNAME
sudo scutil --set LocalHostName $HOSTNAME
sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string $HOSTNAME!youtube(link URL)
Posted on 07-19-2018 02:49 PM
How are you starting the script and how are you delivering it to the machines pre-enrollment?
If you launch it from a launch Daemon for example it should be able to change the Computer Name without asking for a password. You would need to do something like have it run every 20 seconds and check if there was a valid user logged in before prompting though.
Posted on 07-19-2018 03:59 PM
Do you need to rename before enrolling? Renaming after enrolment works okay when followed by a recon, then the computer record name gets updated in JSS. We are using the following script to rename and set department, it writes the data to a plist for future use, also the prompts run as the logged in user otherwise they won't appear. Runs via a policy triggered at enrolment complete for manually enrolled Macs, and via our provisioning script for DEP enrolled Macs.
#!/bin/bash
# Set basic variables
serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}')
loggedInUser=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
loggedInUID=$(id -u $loggedInUser)
# Ask for computer name and rename
computerName=$(/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOF
tell application "System Events"
activate
with timeout of 3600 seconds
set the answer to text returned of (display dialog "Enter Computer Name" default answer "$serial" buttons {"Continue"})
end timeout
end tell
EOF)
echo Computer Name is $computerName
/usr/bin/defaults write /Library/Receipts/JSSData.plist ComputerName $computerName
jamf setComputerName -name "${computerName}"
# Ask for computer department and report to JSS
computerDept=$(/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<EOL
tell application "System Events"
activate
with timeout of 3600 seconds
choose from list {“DEPT1”, “DEPT2”, “DEPT3”} with prompt "Select Department" default items "None" OK button name {"Continue"} cancel button name {"Cancel"}
end timeout
end tell
EOL)
echo Computer Department is $computerDept
/usr/bin/defaults write /Library/Receipts/JSSData.plist Department $computerDept
jamf recon -department $computerDept
exit 0