Skip to main content
Question

Custom Text Field EA filled in with a script?

  • November 27, 2013
  • 8 replies
  • 20 views

Forum|alt.badge.img+20
  • Valued Contributor
  • 732 replies

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

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • November 27, 2013

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


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • November 27, 2013

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


Forum|alt.badge.img+5
  • Contributor
  • 54 replies
  • November 27, 2013

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?


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • November 27, 2013

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


Forum|alt.badge.img+5
  • Contributor
  • 54 replies
  • November 27, 2013

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

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

Hope this helps


Forum|alt.badge.img+20
  • Author
  • Valued Contributor
  • 732 replies
  • November 27, 2013

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


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • November 27, 2013

@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

1#!/bin/sh
2
3## Hardcoded variables
4jssURL="https://yourjss.server.com"
5apiUser="apiuser"
6apiPass="apipass"
7
8## Set EA record name and values here
9ea_name="foobar"
10ea_value="foo"
11
12## Get the Mac's MAC address for en0, to use with pulling the computer's name from the JSS
13MACAdd=$( networksetup -getmacaddress en0 | awk '{ print $3 }' | sed 's/:/./g' )
14
15## Get the Mac's name as it appears in its own record in the JSS
16MacName=$( curl -s -u ${apiUser}:${apiPass} "${jssURL}/JSSResource/computers/macaddress/$MACAdd" | xpath /computer/general/name[1] | awk -F'[<|>]' '{print $3}' )
17
18## Create the xml file
19echo "<?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
20
21## Upload the xml file
22/usr/bin/curl -s -k -u ${apiUser}:${apiPass} "${jssURL}/JSSResource/computers/name/${MacName}" -T "/private/tmp/ea.xml" -X PUT
23
24## Check if the last operation was successful and clean up. Otherwise exit with an error
25if [ "$?" == "0" ]; then
26 rm /private/tmp/ea.xml
27 exit 0
28else
29 echo "Error uploading the xml"
30 exit 1
31fi

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.


Forum|alt.badge.img+12
  • Contributor
  • 61 replies
  • July 19, 2017

@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.

1#!/bin/sh
2## Hardcoded variables
3JSSName="https://xxx.xxx.xxx:8443"
4apiUser="xxxxxxx"
5apiPass="xxxxxxxx"
6
7## Set EA record name and values here
8ea_name="Region Name"
9ea_value=“1”
10
11## Get the Mac's MAC address for en0, to use with pulling the computer's name from the JSS
12#MACAdd=$( networksetup -getmacaddress en0 | awk '{ print $3 }' | sed 's/:/./g' )
13#echo $MACAdd
14## Get the Mac's name
15machinename=$( jamf getComputerName | cut -d ">" -f 2 | cut -d "<" -f 1 )
16echo $machinename
17## Create the xml file
18echo "<?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
19echo 2
20## Upload the xml file
21 curl -v -d -k -u xxxxx:xxxxx "$JSSName/JSSResource/computerextensionattributes/id/17/$machinename" -T "/private/tmp/ea.xml" -X PUT
22echo 3
23## Check if the last operation was successful and clean up. Otherwise exit with an error
24if [ "$?" == "0" ]; then
25 rm /private/tmp/ea.xml
26 exit 0
27else
28 echo "Error uploading the xml"
29 exit 1
30fi

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.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings