Script Help for a newby

edullum
Contributor

I'm trying to pull the asset tag info from the JSS API and apply that to the Computer Info Field 1 on the device. I'm still learning scripting, and need help with my syntax. This is what I have so far...

#!/bin/bash

#Pull Asset Tag from the device
asset_Tag=$ /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}';

#Name the ARD field 1 with the asset tag pulled from JSS
/System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -computerinfo -set1 -1 "$asset_Tag"
2 ACCEPTED SOLUTIONS

easyedc
Valued Contributor II

I'm no scripting guru, but is your variable defined properly? don't we need ( ) in the definition?

asset_Tag=$( /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}'; )

View solution in original post

ryan_ball
Valued Contributor

This will get you the asset tag from the JSS and assign it to a variable. Obviously pass your JSS user credentials as the 4th and 5th parameter or you could hard-code it in place of the $4 and $5.

#!/bin/bash

jamfProURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url)
apiUser="$4"
apiPass="$5"


serial=$(/usr/sbin/system_profiler SPHardwareDataType 2> /dev/null | grep Serial |  awk '{print $NF}')
jamfProId=$(/usr/bin/curl -s -u "${apiUser}:${apiPass}" -H "Accept: application/xml" "${jamfProURL%/}/JSSResource/computers/serialnumber/${serial}/subset/general" | perl -lne 'BEGIN{undef $/} while (/<id>(.*?)</id>/sg){print $1}' | head -1)
asset=$(/usr/bin/curl -s -u "${apiUser}:${apiPass}" -H "Accept: application/xml" "${jamfProURL%/}/JSSResource/computers/id/${jamfProId}/subset/general" | perl -lne 'BEGIN{undef $/} while (/<asset_tag>(.*?)</asset_tag>/sg){print $1}' | head -1)

if [[ -z "$jamfProId" ]] || [[ -z "$asset" ]] || [[ "$asset" =~ (Error|Conflict|Unauthorized|mismatch) ]]; then
        echo "There was an error getting the Asset Tag field from the Jamf Pro Server."
        exit 1
else
        echo "Asset Tag for this machine is $asset"
        exit 0
fi

EDITED - If you have a self-signed (untrusted) cert in your JSS, you'll need to modify the curl commands like so:

/usr/bin/curl -s -k -u

View solution in original post

4 REPLIES 4

Asnyder
Contributor III

You need to do a get from the api. There are a lot of posts on here involving using the api. In the script I would 1. Get the serial number from the computer. 2. Do an api get using the /computers/serialnumber/ endpoint. 3. parse that data to create the asset_Tag variable you can then use for the kickstart.

yourjss.com:8443/api is the place to start.

easyedc
Valued Contributor II

I'm no scripting guru, but is your variable defined properly? don't we need ( ) in the definition?

asset_Tag=$( /usr/bin/awk -F'<asset_tag>|</asset_tag>' '{print $2}'; )

ryan_ball
Valued Contributor

This will get you the asset tag from the JSS and assign it to a variable. Obviously pass your JSS user credentials as the 4th and 5th parameter or you could hard-code it in place of the $4 and $5.

#!/bin/bash

jamfProURL=$(defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url)
apiUser="$4"
apiPass="$5"


serial=$(/usr/sbin/system_profiler SPHardwareDataType 2> /dev/null | grep Serial |  awk '{print $NF}')
jamfProId=$(/usr/bin/curl -s -u "${apiUser}:${apiPass}" -H "Accept: application/xml" "${jamfProURL%/}/JSSResource/computers/serialnumber/${serial}/subset/general" | perl -lne 'BEGIN{undef $/} while (/<id>(.*?)</id>/sg){print $1}' | head -1)
asset=$(/usr/bin/curl -s -u "${apiUser}:${apiPass}" -H "Accept: application/xml" "${jamfProURL%/}/JSSResource/computers/id/${jamfProId}/subset/general" | perl -lne 'BEGIN{undef $/} while (/<asset_tag>(.*?)</asset_tag>/sg){print $1}' | head -1)

if [[ -z "$jamfProId" ]] || [[ -z "$asset" ]] || [[ "$asset" =~ (Error|Conflict|Unauthorized|mismatch) ]]; then
        echo "There was an error getting the Asset Tag field from the Jamf Pro Server."
        exit 1
else
        echo "Asset Tag for this machine is $asset"
        exit 0
fi

EDITED - If you have a self-signed (untrusted) cert in your JSS, you'll need to modify the curl commands like so:

/usr/bin/curl -s -k -u

edullum
Contributor

Thank you thank you!!!@ryan.ball and @easyedc