Custom Text Field EA filled in with a script?

Matt
Valued Contributor

Hey everyone. I made a custom Extension Attribute that is a blank text field. I would like to write a script that fills that field in during imaging. How would one call this custom field and fill it?

Thanks!

8 REPLIES 8

mm2270
Legendary Contributor III

Probably via the JSS API. Take a look at this thread where @derek.brost explains the process. Basically it involves creating an xml file to upload to the JSS via the API that would update the computer record with the custom information.
I think this could be done for manually set EAs.

https://jamfnation.jamfsoftware.com/discussion.html?id=8943

Matt
Valued Contributor

I have no clue how to use the API and the instructions are a bit lacking. :( I understand the whole XML bit but from where to where and how :D

evarona
New Contributor II

Not sure what the use case is here but from my understanding, I'd probably leave a breadcrumb somewhere on the disk and have the EA read it. Say you wanted to see if your custom files showed up in the build. Write a script to check for the files and leave a marker somewhere. Then tie the script to a one time startup policy. The EA can check for the breadcrumb your script leaves behind.

Is that what you're after?

Matt
Valued Contributor

Im trying to label my images with build numbers so we can find out who has what image.

evarona
New Contributor II

Ah, got it. I go for the KISS approach. I'd do something create a file like /private/tmp/buildver.txt, bake that into each build and put nothing but the number there. Then have the EA read it like this

#!/bin/bash
buildVer = `cat /private/tmp/buildver.txt`
echo -n "<result>${buildVer}</result>"

Hope this helps

Matt
Valued Contributor

I thought about doing it this way. Let me give it a shot!

mm2270
Legendary Contributor III

@Matt - OK, so, if you still want to go the route of updating via the API, you can try the following.

Looking at what derek wrote, lets make a few assumptions. First, let's assume your custom EA name is "foobar" and the value you want to write into the Mac's JSS record for that EA is "foo" Let's also assume you have an API enabled account on your JSS. Preferably something that can only read/write from the API and not log into the JSS console. (the username & password will be hardcoded into the script)
Assuming all the above, we can create a script that would:
a) Pull the Mac's name as the JSS sees it (you need either the Mac name or the JSS ID for the PUT command)
b) Create an xml file with values you want and save to disk somewhere on the Mac itself
c) upload the aforementioned xml file with the PUT command, using the Mac's name or JSS ID as the entry to update
* technically you could pull the Mac's name from the Mac itself, but if it has any odd characters, what you pull may not match the entry in the JSS exactly, and it must match exactly, or, use the JSS ID*

Keep in mind that to do any of this, the Mac has to already be enrolled in your JSS, so this would need to happen after imaging is done, perhaps during a first run script or something after that.

Here's an example script. I can use the following, with our own information entered of course, to update the only manual EA we have in our JSS. I tested this and it works.
Change any of the variables, like apiuser, apipass, ea_name, etc to the values you need

#!/bin/sh

## Hardcoded variables
jssURL="https://yourjss.server.com"
apiUser="apiuser"
apiPass="apipass"

## Set EA record name and values here
ea_name="foobar"
ea_value="foo"

## Get the Mac's MAC address for en0, to use with pulling the computer's name from the JSS
MACAdd=$( networksetup -getmacaddress en0 | awk '{ print $3 }' | sed 's/:/./g' )

## Get the Mac's name as it appears in its own record in the JSS
MacName=$( curl -s -u ${apiUser}:${apiPass} "${jssURL}/JSSResource/computers/macaddress/$MACAdd" | xpath /computer/general/name[1] | awk -F'[<|>]' '{print $3}' )

## Create the xml file
echo "<?xml version="1.0" encoding="UTF-8" standalone="no"?><computer><extension_attributes><attribute><name>${ea_name}</name><value>${ea_value}</value></attribute></extension_attributes></computer>" > /private/tmp/ea.xml

## Upload the xml file
/usr/bin/curl -s -k -u ${apiUser}:${apiPass} "${jssURL}/JSSResource/computers/name/${MacName}" -T "/private/tmp/ea.xml" -X PUT

## Check if the last operation was successful and clean up. Otherwise exit with an error
if [ "$?" == "0" ]; then
    rm /private/tmp/ea.xml
    exit 0
else
    echo "Error uploading the xml"
    exit 1
fi

All that said, if there's a way to use the breadcrumb approach, that may be easier to implement. However, in the interest of sharing information, I just thought I'd post on how you could do it using the API method.

ifbell
Contributor

@mm2270 So I looked at your code above and it looks like what I need for my current project but I am unable to get it to post anything into the EA that I created.

#!/bin/sh
## Hardcoded variables
JSSName="https://xxx.xxx.xxx:8443"
apiUser="xxxxxxx"
apiPass="xxxxxxxx"

## Set EA record name and values here
ea_name="Region Name"
ea_value=“1”

## Get the Mac's MAC address for en0, to use with pulling the computer's name from the JSS
#MACAdd=$( networksetup -getmacaddress en0 | awk '{ print $3 }' | sed 's/:/./g' )
#echo $MACAdd
## Get the Mac's name
machinename=$( jamf getComputerName | cut -d ">" -f 2 | cut -d "<" -f 1 )
echo $machinename
## Create the xml file
echo "<?xml version="1.0" encoding="UTF-8" standalone="no"?><computer><extension_attributes><attribute><name>${ea_name}</name><value>${ea_value}</value></attribute></extension_attributes></computer>" > /private/tmp/ea.xml
echo 2
## Upload the xml file
 curl -v -d  -k -u  xxxxx:xxxxx "$JSSName/JSSResource/computerextensionattributes/id/17/$machinename" -T "/private/tmp/ea.xml" -X PUT
echo 3
## Check if the last operation was successful and clean up. Otherwise exit with an error
if [ "$?" == "0" ]; then
    rm /private/tmp/ea.xml
    exit 0
else
    echo "Error uploading the xml"
    exit 1
fi

So what I am getting back is.

2 <html> <head> <title>Status page</title> </head> <body style="font-family: sans-serif;"> <p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Not Found</p> <p>The server has not found anything matching the request URI</p> <p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">here</a>.<br> Please continue your visit at our <a href="/">home page</a>. </p> </body> </html> 3

So I know where the issue is I just am not sure what I am missing in that curl statement. The Machine name matches what is in the JSS.