Script to set Asset Name to Computer name and enable Dynamic Host Name

New Contributor III

Hi All,


I am fairly new to scripting and am humbly requesting help.  

We use the following script to rename the Computer Name to Asset Name(via what is uploaded in Inventory Preload).  What I found, is I also need this script to enable Dynamic Host Name and set that name to that same Asset Name.  I cannot get that last piece to work and believe it was incorrect, so I have removed it and asking the community.


Here is the script I am using to set Computer Name to Asset, which has been working great.  I am hoping to be able to use this same script and add the piece to enable naming of the Dynamic Host name as well.




#set the variables for the server and API account


#get the serial number

serialNumber="$(ioreg -l | grep IOPlatformSerialNumber | sed -e 's/.*\"\(.*\)\"/\1/')"

#get the asset tag from jamf

assetTag=$(/usr/bin/curl -H "Accept: text/xml" -sfku "${jssUser}:${jssPass}" "${jssHost}/JSSResource/computers/serialnumber/${serialNumber}/subset/general" | xmllint --format - 2>/dev/null | awk -F'>|<' '/<asset_tag>/{print $3}')

# set computer name

if [ "$assetTag" == "" ]; then
echo "Asset Tag is empty. Exiting..."
exit 1
/usr/sbin/scutil --set HostName "$assetTag"
/usr/sbin/scutil --set LocalHostName "$assetTag"
/usr/sbin/scutil --set ComputerName "$assetTag"

/usr/local/jamf/bin/jamf recon



I appreciate any help!  


Valued Contributor II

try adding this line: 

sudo defaults write /Library/Preferences/SystemConfiguration/ NetBIOSName "$assetTag"

New Contributor III

Thank you!  I will give this a shot.  One question, I assume since this is running this with sudo privileges, I would have to modify my API account possibly, or should it be ok?


Valued Contributor II

it will be fine since the script is running as root.  

Contributor II

Just an aside, I would highly recommend setting up brysontyrrell/EncryptedStrings instead of including your password in plain text. Jamf Pro scripts are downloaded and stored locally when run so any password you include is easily accessible to a user. The benifit here is that an attacker would need access to a local device as well as your Jamf instance to get the credentials. 

New Contributor III

Thanks Trent.  Curious if there is any more documentation on how to use the encrypted strings?  I am new to scripting. 

Contributor III

@DanVT - I was going to suggest you have a serious conversation about the utility of asset tags and naming your computers with them, as I suspect - unless you're paying extra to some vendor to get your Macs' asset tags loaded into Jamf Pro that you've got a manual process and relying on API lookup that is fraught with risk. Better to name based on model and owner or serial number or some other designation you can derive from the device programmatically as opposed to a sticker on the outside - that's for the physical inventory people.