Script to name and add asset tag

New Contributor

We have been using a script to initiate in self service that asks for the name of the mac and to add asset tag info to jamf. It seems to have stopped working and not sure why exactly. The naming portion seems to work but it does not reflect in the Jamf inventory entry every time. Can someone have a look at this and tell me why or what changed to make it no longer work? It's been in use for 2 years with no issues.


#defining the jamf binary
jamf=$(which jamf)

#asking the logged in user via apple script dialogue box for the asset tag on the machine
assetTag=$(/usr/bin/osascript <<-'__EOF__'
text returned of (display dialog "Please type in your 6 digit asset tag for this computer.(Inventory Decal)" default answer "######" buttons {"OK", "Cancel"} default button 1 with icon file "Applications:System")

#asking the logged in user via apple script dialogue box for the name of the machine
PreFix=$(osascript <<'EOF'
text returned of (display dialog "Please type in your Computer name for this machine" default answer "" buttons {"OK", "Cancel"} default button 1)

# Set Hostname using variable created above
scutil --set HostName "$PreFix"
scutil --set LocalHostName "$PreFix"
scutil --set ComputerName "$PreFix"

#setting the compuer assettag in Jamf and the computer name in Jamf
"$jamf" recon -assetTag "$assetTag"
"$jamf" setComputerName -name "$PreFix"

exit 0




I use a similar script but mine uses the Asset Tag value to rename the computer. I did however during writing and testing this try and use both the Jamf command to set the computer name and the Jamf command to set the Asset Tag. My script does not ask the user to enter anything.

I can therefore say that it is strictly speaking not necessary to use both the scutil commands as well as the "$jamf" setComputerName -name "$PreFix" command as the Jamf command itself will do the scutil commands for you as part of renaming the computer and updating the Jamf Pro entry.

My own use of the Jamf recon command was as follows

"$jamf" recon -skipApps -skipFonts -skipPlugins -assetTag "$asset"

This is as far as I can see equivalent to yours and therefore I would have expected yours to work. Mine merely skips the other recon tasks so it only does the updating of the Asset Tag field.

I would suggest that as always when writing and testing scripts you should do so with the minimum code need to reproduce a desired goal. Only at the end would you combine everything together. Therefore I would suggest trying the following only

"$jamf" recon -skipApps -skipFonts -skipPlugins -assetTag "TEST123"

You could even try an equivalent of this interactively in without needing a Jamf policy and script at all. Once you get this bit working then combine it with other parts of your script.

Oddly enough moving the asset tag portion of the script under the computer name portion fixed the issue. Odd.