When I switched to Apple’s DEP deployment program there became a need to import the asset data for our devices. Previously asset tags were assigned to a device, then during the manual imaging process the name was input into Casper Imaging, a Jamf Pro product.

A major part of the appeal of DEP is the ability to hand off the device to your user straight away, fresh, out of the box. IT never has to lay a hand on the device, but you do need to do a bit of work in the background to make it all flow smoothly. I’ll elaborate on the DEP process in another post.

The script below is one that I use after the devices have been received by the users and setup. After the setup process the device has a name like “Jacobs Macbook Air”, but we want it to have the asset tag we’ve assigned in our asset management software.

The script takes a csv file arranged in the following order.
Serial Number, Asset Tag, Username

It is quite specific to my environment, but you could easily adapt it to suit your own requirements.

# Variables go here. API account can't write to the JSS directly so don't try, it'll fail
apiUsername='zzz'
apiPassword='zzz'
jssServer='https://zzz.com:443'
file="$1"

#Verify we can read the file
data=`cat $file`
if [[ "$data" == "" ]]; then
    echo "Unable to read the file path specified"
    echo "Ensure there are no spaces and that the path is correct"
    exit 1
fi

#Find how many computers to import
computerqty=`awk -F, 'END {printf "%s\n", NR}' $file`
#computerqty=$((computerqty-1))
echo "Preparing to update $computerqty computers"
#Set a counter for the loop
counter="0"

duplicates=[]

id=$((id+1))

#Loop through the CSV and submit data to the API
while [ $counter -lt $computerqty ]
do
    counter=$[$counter+1]
    line=`echo "$data" | head -n $counter | tail -n 1`
    serialNumber=`echo "$line" | awk -F , '{print $1}'`
    assetTag=`echo "$line" | awk -F , '{print $2}'`
    user=`echo "$line" | awk -F , '{print $3}'`
    echo $serialNumber
    echo $assetTag
    echo $user
	echo "##########################################"
    echo "Attempting to update data for $serialNumber"

	apiData="$assetTag"
	userName="$user"
	output=`curl -s -k -u "${apiUsername}:${apiPassword}" -H "Content-Type: application/xml" -X 'PUT' -H "Content-Type: text/xml" -d "$apiData" "${jssServer}/JSSResource/computers/serialnumber/${serialNumber}"`
	addUserName=`curl -s -k -u "${apiUsername}:${apiPassword}" -H "Content-Type: application/xml" -X 'PUT' -H "Content-Type: text/xml" -d "$userName" "${jssServer}/JSSResource/computers/serialnumber/${serialNumber}"`
	#echo $output
	echo "Serial number is: $serialNumber"
	echo "Asset Tag is: $assetTag"
	echo "Username is: $user"
	#echo "Api Data is: ${apiData}"

    
    #Error Checking
    error=""
    error=`echo $output | grep "Conflict"`
    if [[ $error != "" ]]; then
        duplicates+=($serialNumber)
    fi
    #Increment the ID variable for the next user
    id=$((id+1))

done

echo "The following computers could not be updated:"
printf -- '%s\n' "${duplicates[@]}"

exit 0