Skip to main content

I am running a script that will ask the end user to enter the machine name. It will not work through Casper (remotely or in a policy) but will work fine if I run it locally. Any ideas why it may not be running? I have included the script below.



!/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
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName



echo Rename Successful



exit



Very odd the script runs fine when ran locally through the terminal. But stalls and never runs and never errors out when ran through a policy or through remote.



Thank you.

I have a very similar script but I run mine via Self Service.


I am trying to run it during prestage so it needs to run when the Mac gets placed into the JSS. It will make call to it but it never runs the script. So I tried it by itself not running during that process and it doesn't work. I do not want the end user to have to go into self service.


I see you are calling 'osascript'. A policy run via the JSS does not have access to a user's working environment. That makes things like notification windows or running a launch agent very difficult. Jamfers have typically used bsexec or sudo as user to try.



If you run the script though Self Service, it is rooted in the user's process, so then it is allowed to create the dialog boxes you are looking for.



To fix this, search for some postings about notifications to users and how they do it. With 10.11, I've pretty much given up trying to fight that dragon. or actually, I just use jamfHelper which seems to usually be able to present to the user without trouble.


This is what I use in my DEP workflow to prompt for naming the machine. Maybe you can adapt it to your needs. I don't set the host name because I have another script that runs on all machines that sets the host name from the ComputerName (this resolves some issues with EPO)



#!/bin/bash

/usr/bin/osascript << EOF
property compName : ""

repeat while compName is ""
tell application "Finder"
activate
display dialog "What should this computer be named:" default answer compName
set compName to text returned of result
end tell
end repeat

try
do shell script "hostname " & quoted form of compName
on error errorMsg number errorNum
display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try
EOF

name=$(hostname)
scutil --set ComputerName "${name}"
scutil --set LocalHostName "${name}"


  1. You need quotes in the echo "Rename Successful"



Otherwise, this worked as expected for me as a policy.



#!/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
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName

echo "Rename Successful"

exit 0

I use a script which leverages CocoaDialog to prompt local support for the new machine name and accomplishes a re-bind to AD via a custom policy trigger. This policy has an Update Inventory on it already, so its not in this script:



#!/bin/sh
########################################################################
# Created By: Ross Derewianko Ping Identity Corporation
# Creation Date: February, 2015
# Last modified: December 14th, 2015
# Modified for Sapient: December 14th, 2015 - Daniel Greening
# Brief Description: Changes machine hostname
########################################################################

#check for CocoaDialog & if not install it

if [ -d "/Library/Application Support/JAMF/bin/CocoaDialog.app" ]; then
CoDi="/Library/Application Support/JAMF/bin/CocoaDialog.app/Contents/MacOS/cocoaDialog"
else
echo "CocoaDialog.app not found installing"
jamf policy -event cocoa
CoDi="/Library/Application Support/JAMF/bin/CocoaDialog.app/Contents/MacOS/cocoaDialog"
fi

########################################################################
# Functions
#######################################################################

#asks for the new hostname & then call in the cleaner!

function cdprompt() {
hostname=`"$CoDi" standard-inputbox --float --title "Sapient LS Computer Rename Utility" --informative-text "Enter the new computer name using Sapient naming convention:"`

if [ "$hostname" == "2" ]; then
echo "user cancelled"
exit 1
fi
cleanhostname
}

#cleans the first two characters out (cocoaDialog adds a 1
to the string value which we don't need.)

function cleanhostname() {
hostname=${hostname:2}
}

#checks for a blank hostname, and if its blank prompt agian

function checkforblank() {
while [[ -z $hostname && {$hostname+1} ]]
do
cdprompt
done
}

function sethostname() {
scutil --set HostName $hostname
scutil --set ComputerName $hostname
scutil --set LocalHostName $hostname
}

########################################################################
# Script
########################################################################

cdprompt
checkforblank
sethostname
jamf policy -event ADBind

Here's yet another way it could be written (this one was just more for fun)
Your version pasted was problematic... You need to paste the script with three ticks (the key beside 1) on the front end of the script and the end. Otherwise markup gets in the way.



#!/bin/bash
###functions
function machinename () {
osascript <<EOT
tell application "Finder"
activate
set nameentry to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)
end tell
EOT
}

function renameComputer(){
#Set New Computer Name
echo "The New Computer name is: $ComputerName"
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName

echo Rename Successful
}

###Script
ComputerName=$(machinename)
renameComputer
exit 0


```


The problem is the Applescript (osascript) call in your script. Applescript messages that call for user interaction don't work well when they are done via a root session, the way a Casper Suite policy will do. Its not Casper's fault. Its the OS doing its job of protecting the user space. Essentially osascript commands called by root can't display to the logged in user (unless the user logged in is root of course)



There are a few ways around it. You can try calling the command as the user with a sudo -u $loggedInUser type syntax, but sometimes even this doesn't work. The more reliable way would be to use launchctl asuser (10.10 & 10.11), orlaunchctl bsexec (10.9 and below)



Another way would be to ditch Applescript and use something like cocoaDialog, which doesn't run into the same restrictions. But this would mean deploying a custom tool to your Macs. You may not want to do that, and I'd understand. Though cocoaDialog, while old now, is still very useful in many regards.



For using launchctl, you can try something like this. Keep in mind this will only work on 10.10 and 10.11. The 'asuser' syntax doesn't exist in earlier OSes.



#!/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 set ComputerName to text returned of (display dialog "Please Input New Computer Name" default answer "" with icon 2)'")

echo "$ComputerName"


Just fill out the remainder of the script with the rest of what you wrote. The above should work, but I only wrote this right here and didn't test it, so give it a try.


@dgreening I love that script.


Your script errored out as well. It did work if ran locally but not through Remote.


Hi @macboy Whose script were you referring to?


Sorry I was referring to @Kaltsas .


It runs locally on a machine after a DEP machine is enrolled so that seems plausible. It's kind of a kludge and I'm already seeing way better ways to do this in this thread than I am.


That seems to work @mm2270 . It worked for me remotely now I will try it through a policy but imagine it will work fine. Thank you so much.


@dgreening I love that script ;)


@rderewianko Yes! Thanks for creating the original! It works GREAT (and ensures that our local support staff does not skip steps in the rename process)!


I recently published another version I've been toying with. For renaming a bound machine (and changing the name in AD) Here


@rderewianko Dead link. :)


Dangit
Its due to the bracket in the URL!


I keep seeing mixed comments regarding renaming machines. I see some stating to do the following...



scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName



Then I've read some posts that state to only do...



scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName



Those same people say to never set HostName. Does anyone here know why and can explain it in plain english?


@jhuls I believe, and someone correct me please if I am wrong, that changing the HostName will affect your Active Directory bind if you are bound to AD. I believe that the HostName is the name that you see in AD, and if your computer is bound when you change it, you could break that bind.


Have folks had success with this type of script in a macOS10.13+ deployment?


If you're talking about the one above, Here



Cocoa Dialogue does not work on 10.13 there is a new version in development.
Here but there has yet to be a release.


I'm doing this with a one liner in a Policy under Files and Processes -> Execute command



var=$(osascript -e 'tell application "Finder" to set CompName to text returned of (display dialog "Enter the proper machine name" with title "Improper Machine Name" default answer"")') && var2=$(echo "$var" | tr '[:lower:]' '[:upper:]') && jamf setComputerName -name $var2



This pops up a simple prompt for a name, uppercases it, and sets it using the jamf command that takes care of all 3 of the commands others have mentioned using. I have this Policy scoped to a smart group based on name prefix. The only down side so far is that there seems to be a 20 min delay until the console reflects this new name, which means the prompt may come up again.


Anyone had success doing this for domain bound machines with AD (mobile managed) user accounts?