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.

Hello folks,



I've been looking to simplify the host naming step for the DEP workflow and for the IT support functions. Now, only part that eludes me in Applescript is how have I could have more describing way for locations as standard user does not understand what the prefix means.



See below:



#!/bin/bash

## Rename device - draft


## Set prefix value for the region

ComputerName=`/usr/bin/osascript <<EOT

set AssetTag to text returned of (display dialog "please insert asset tag" default answer "" with icon 2)

tell application "System Events"
activate
set ComputerLocations to {"BE1MCL", "NL1MCL", "NL2MCL","PL1MCL"}
set ComputerName to choose from list ComputerLocations with prompt "Select your office location:" default items {"NL1MCL"}
end tell
EOT`

## User input for the security sticker/asset tag
AssetTag=`/usr/bin/osascript <<EOT
tell application "System Events"
activate

set AssetTag to text returned of (display dialog "Please insert the company provided asset tag" default answer "" with icon 2)
end tell
EOT`

##Set New Computer Name

echo $ComputerName
echo $AssetTag
sudo scutil --set HostName $ComputerName-$AssetTag
sudo scutil --set LocalHostName $ComputerName-$AssetTag
sudo scutil --set ComputerName $ComputerName-$AssetTag
sudo /usr/local/bin/jamf setComputerName -name $ComputerName-$AssetTag

echo Rename Successful

@Xerendor If you only have the four locations, then you could prompt for the actual location name in English for the user to choose, then do an IF THEN set of statements to get the ComputerName you want.


Could use some help on ours. Basically, we need it unbind the Mac once logged in, pop up and ask for the Asset Tag number. Once it has the Asset Tag number I want it to then ask if the computer is mobile or desktop. Once that is determined, it'll add -M to the Asset Tag number to name a laptop, or leave the name without the -M if the asset is a Desktop. After all this it triggers my bind script and rebinds with the correct name.



I have gotten the script to work just fine (including adding the -M to the name) without the if/else statement but once I add the if/else statement it hangs on Running in Self Service. Unfortunately, I am just not familiar enough with if/then statements to know what I am doing wrong.



Here is my script:



```
# Unbind the AD

sudo dsconfigad -force -remove -u johndoe -p nopasswordhere

# Prompt User for Asset Tag

assetTag=$(`/usr/bin/osascript <<EOT
tell application "System Events"
activate
set assetTag to text returned of (display dialog "Please Enter the Asset Tag Number for this Device." default answer "")
end tell
EOT`
)

# Prompt User for Asset Type

assetType=$(`/usr/bin/osascript <<EOT
tell application "System Events"
activate
set assetType to text returned of (display alert "Is This Asset a Laptop?" buttons {"Yes", "No"})
end tell
EOT`
)


# Name According to Asset Type

assetName=$(`/usr/bin/osascript <<EOT
if assetType contains "Yes" then
set assetName to (assetTag as text)'-M'
else if assetType contains "No" then
set assetName to (assetTag as text)
end if
EOT`
)

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

# Submit New Name to Jamf

sudo jamf setComputerName -name $assetName

# Submit Asset Tag to Jamf

sudo jamf recon -assetTag $assetTag

# Update Jamf inventory data

sudo jamf recon

# Notify User of Rename Success

`/usr/bin/osascript <<EOT
tell application "System Events"
activate
display dialog "Rename Successful!"
end tell
EOT`


```


My previous script - the one that worked but did not determine between Laptop/Desktop is this:



Unbind the AD



sudo dsconfigad -force -remove -u johndoe -p nopasswordhere



Prompt User for Asset Tag



while :; do



assetTag=$(osascript -e 'tell application "System Events" to display dialog "Please Enter the Asset Tag Number for this Device." default answer ""' -e 'text returned of result' 2>/dev/null)



Set Asset Name



assetName=$(echo $assetTag)'-M'



Tell System Events to loop if input is empty



if [[ -z "$assetTag" ]]; then



# The user left the name blank
osascript -e 'Tell application "System Events" to display alert "You must enter an Asset Tag Number. Please try again." as warning' >/dev/null



# Continue loop to prompt again.
else
# Valid input: exit loop and continue.
break
fi
done



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



Make sure Jamf Updates with New Name



sudo jamf setComputerName -name $assetName



Submit Asset Tag to Jamf



sudo jamf recon -assetTag $assetTag



Update Jamf inventory data



sudo jamf recon



Notify User of Rename Success



osascript -e 'Tell application "System Events" to display dialog "Rename Successful!"'


@josh.glenn



You can get the model from the Mac, no need to ask the user for it.
As you are running a bash/shell script there is no need to run the ifs in AppleScript



Modified script:



```
# Unbind the AD
sudo dsconfigad -force -remove -u johndoe -p nopasswordhere

# Prompt User for Asset Tag
assetTag=$(`/usr/bin/osascript <<EOT
tell application "System Events"
activate
set assetTag to text returned of (display dialog "Please Enter the Asset Tag Number for this Device." default answer "")
end tell
EOT`
)

# Name According to Asset Type
# looking for Book or book.
hdModel=$(sysctl hw.model | awk 'BEGIN {} $2~/.(b|B)ook./{print "M"}')

# if it is a "book" add the '-M' into the assetName
if [ "$hdModel" == "M" ];then
assetName="$assetName-M"
fi

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

# Submit New Name to Jamf
sudo jamf setComputerName -name $assetName

# Submit Asset Tag to Jamf
sudo jamf recon -assetTag $assetTag

# Update Jamf inventory data
sudo jamf recon

# Notify User of Rename Success
`/usr/bin/osascript <<EOT
tell application "System Events"
activate
display dialog "Rename Successful!"
end tell
EOT`


Regards


I've cobbled together the following based upon the work of a lot of smarter people both here on the forums and on the general Internet; what's interesting to me is that while the scutil commands run locally in terminal produce the desired result, while run remotely things appear successful, e.g., no errors are logged, but nothing gets changed... It's frustrating to say the least!

 

#!/bin/zsh -v

sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
macModel=$(/usr/sbin/sysctl -n hw.model)
headCriteria="$[insert_your_departmental_designation_here]"
domain="$[insert_your_domain_here]"

# Need to define the non MacBooks options (in our enterprise we use "L" to signify laptops; "D" is for desktops)
if [[ "$macModel" =~ BookAir ]]; then
setModel='L'
elif [[ "$macModel" =~ BookPro ]]; then
setModel='L'
elif [[ "$macModel" =~ Book ]]; then
setModel='L'
else
setModel='D'
fi

ComputerName="$headCriteria-$sn-$setModel"
HostName="$headCriteria-$sn-$setModel-$domain"
LocalHostName="$headCriteria-$sn-$setModel"

# Set the ComputerName, HostName and LocalHostName
sudo /usr/sbin/scutil --set ComputerName "$computerName"
sudo /usr/sbin/scutil --set HostName "$hostName"
sudo /usr/sbin/scutil --set LocalHostName "$localHostName"

## Create dummy receipt to mark complete
touch /Library/Receipts/com.company.renameComplete.bom

## Update Inventory
/usr/local/bin/jamf recon

 

It runs, appears to complete, and seemingly does nothing!


I've cobbled together the following based upon the work of a lot of smarter people both here on the forums and on the general Internet; what's interesting to me is that while the scutil commands run locally in terminal produce the desired result, while run remotely things appear successful, e.g., no errors are logged, but nothing gets changed... It's frustrating to say the least!

 

#!/bin/zsh -v

sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
macModel=$(/usr/sbin/sysctl -n hw.model)
headCriteria="$[insert_your_departmental_designation_here]"
domain="$[insert_your_domain_here]"

# Need to define the non MacBooks options (in our enterprise we use "L" to signify laptops; "D" is for desktops)
if [[ "$macModel" =~ BookAir ]]; then
setModel='L'
elif [[ "$macModel" =~ BookPro ]]; then
setModel='L'
elif [[ "$macModel" =~ Book ]]; then
setModel='L'
else
setModel='D'
fi

ComputerName="$headCriteria-$sn-$setModel"
HostName="$headCriteria-$sn-$setModel-$domain"
LocalHostName="$headCriteria-$sn-$setModel"

# Set the ComputerName, HostName and LocalHostName
sudo /usr/sbin/scutil --set ComputerName "$computerName"
sudo /usr/sbin/scutil --set HostName "$hostName"
sudo /usr/sbin/scutil --set LocalHostName "$localHostName"

## Create dummy receipt to mark complete
touch /Library/Receipts/com.company.renameComplete.bom

## Update Inventory
/usr/local/bin/jamf recon

 

It runs, appears to complete, and seemingly does nothing!


Hello 2 observations:

1. As the script is run from Jamf, there is no need for sudo.

2. Assuming the variables "headCriteria" and "domain" are handled by another process the issue is the variable names mismatch in this section:

 

# Set the ComputerName, HostName and LocalHostName
/usr/sbin/scutil --set ComputerName "$ComputerName"
/usr/sbin/scutil --set HostName "$HostName"
/usr/sbin/scutil --set LocalHostName "$LocalHostName"