Computer Name and Computer Type Prompts Not Working

jon_hom
New Contributor

Hi Everyone,

Can anybody tell why this script isn't working? As part of our deployment, we're trying to name the computers and set whether it's a Faculty or Student machine. If it's Faculty, we enter the Faculty user. If it's Student, it just sets the Department as "Student".

Oddly, it seems to go right to "You must enter a Computer Name please try again" (without even prompting to enter it first). It then skips the Machine Type part, and is defaulting to Faculty Machine (which then prompts to enter the Faculty user).

For another wrinkle, it seems to work as expected when we do it from Terminal, but as part of the deployment, it skips around.

Any help would be greatly appreciated!

#!/bin/bash

###
#
#Computer Name Setup
#
###


# Loop until valid input is entered or Cancel is pressed.
while : 
do
    computerName=$(osascript -e 'Tell application "System Events" to display dialog "Computer Name" default answer ""' -e 'text returned of result' 2>/dev/null)

    #if (( $? ));
    #    then exit 1; fi  # Abort, if user pressed Cancel.

    computerName=$(echo "$computerName" | sed 's/^ *//' | sed 's/ *$//')  # Trim leading and trailing whitespace.

    computerName=$(echo "$computerName" | sed 's/ /-/g')  # Replace spaces with -

    if [ -z "$computerName" ]
    then
        # The user left the name blank
        osascript -e 'Tell application "System Events" to display alert "You must enter a computer name. Please try again" as warning' >/dev/null
        # Continue loop to prompt again.
    else
        # Valid input: exit loop and continue.
        break
    fi
done



computerType=$(osascript -e 'return choose from list {"Students", "Faculty-Staff"} with title "Machine Type" with prompt "Please select machine type" default items "Students" empty selection allowed false' 2>/dev/null)

#if [ "$computerType" = "false" ]] ;
#   then exit 1; fi  # Abort, if user pressed Cancel.

if [ "$computerType" = "Students" ]
then
     #sets computer department to student in JamfPro
      echo $computerType
      sudo jamf recon -department $computerType
      sleep 7
else
      userName=$(osascript -e 'Tell application "System Events" to display dialog "Enter Faculty/Staff User Name" default answer ""' -e 'text returned of result' 2>/dev/null)
      echo $userName
      sudo jamf recon -endUsername $userName
      sleep 7
fi

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

exit 0
6 REPLIES 6

dan-snelson
Valued Contributor II

@jon.hom

Here's what we're using …

#!/bin/sh
####################################################################################################
#
# ABOUT
#
#   Rename Computer
#
####################################################################################################
#
# HISTORY
#
#   Version 1.0, 18-Jun-2015, Dan K. Snelson
#       Original version
#
####################################################################################################


echo "*** Rename Computer ***"

### Log current computer name
currentComputerName=$( /usr/sbin/scutil --get ComputerName )
echo "Current Computer Name: $currentComputerName"


### Prompt for new computer name
newComputerName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Enter the new computer name:" default answer "" buttons {"Rename","Cancel"} default button 2' -e 'text returned of result' 2>/dev/null)"
if [ $? -ne 0 ]; then
    # The user pressed Cancel
    echo "User clicked Cancel"
    exit 1 # exit with an error status
elif [ -z "$newComputerName" ]; then
    # The user left the computer name blank
    echo "User left the computer name blank"
    /usr/bin/osascript -e 'Tell application "System Events" to display alert "No computer name entered; cancelling." as critical'
    exit 1 # exit with an error status
fi


### Set and log new computer name
/usr/sbin/scutil --set ComputerName "$newComputerName"
echo "New Computer Name: $newComputerName"

### Update the JSS
/usr/local/jamf/bin/jamf recon

# Inform user of computer renamed
jamfDisplayMessage "Renamed computer from: "$currentComputerName" to "$newComputerName""

echo "Renamed computer from: "$currentComputerName" to "$newComputerName""

exit 0      ## Success
exit 1      ## Failure

… also, double-check your privacy settings; see: Preparing Your Organization for User Data Protections on macOS 10.14

guliciuk
New Contributor III

@dan-snelson Hi.
I can't figure out an issue. The script runs well from self service, but if i try to run with different triggers ( custom , by another script, or "at login" ) it will just give me an "User clicked Cancel" response , without any prompt message. I've given pre-approval for apple events , ( given to com.jamf.management.Jamf, /usr/local/jamf/bin/jamfAgent, /usr/local/jamf/bin/jamf). Initially i thought that it's because it's being triggered at login, so i tried triggering with another script , only after DOCK process is active, and the user login process is finished, but still , same error. Any ideeas?

dan-snelson
Valued Contributor II

@guliciuk If the script works from Self Service, but fails with other triggers, it's most likely a missing, odd-ball PPPC identifier.

I'm a big, big fan of Taccy by Howard Oakley (@howardnoakley). Review the "Test TCC" section from the TaccyHelp.pdf including in the download.

In a nutshell, you'll launch Taccy, reproduce the issue, and then view Taccy's logs to see which Jamf process is actually calling the policy and ensure it's included in your PPPC Configuration Profile.

In case it helps, here's all the Jamf-related identifiers I'm using in an unrelated Enterprise Connect-specific PPPC Configuration Profile:
- /usr/local/jamf/bin/jamf
- /usr/local/jamf/bin/jamfAgent
- com.jamf.management.Jamf
- com.jamf.management.service
- /Library/Application Support/JAMF/Jamf.app/Contents/MacOS/Jamf

mm2270
Legendary Contributor III

If I had to take a guess, I'd say what you're running into is that the prompts that ask for user input are getting blocked by the OS, especially when called by other triggers besides Self Service. If I gets run by root, it may not show those prompts, since they require actual input from the logged in user. The dialogs that have static messages don't get the same level of scrutiny by the OS since they are just messages and can't capture text input from the user.

The way to fix that, usually, is to call the dialog as the logged in user instead of root. I've used the launchctl asuser syntax in a number of my scripts to get around that issue. No guarantees it will work, but it generally does. These days there may be more reliable or Apple sanctioned ways to do it though.

guliciuk
New Contributor III

i was also thinking that the error might be in one of this directions

I'd say what you're running into is that the prompts that ask for user input are getting blocked by the OS

or

it's most likely a missing, odd-ball PPPC identifier.

.

@dan-snelson Taacy sounds like a great tool, i'll see what it has to say in this case immediately. Thanks for the fast response!
@mm2270 i'll dig into launchctl asuser method. I've never used it, but it sounds it might be very useful in some situations ( like the one here). Thanks for the ideea !

guliciuk
New Contributor III

Short update.
Found the issue, i needed to run the prompt with the logged in user in order to generate a window.

my fix:

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 + " ");'

newComputerName="$(sudo -u $loggedInUser /usr/bin/osascript -e

Thanks for the help!