I'm wondering if there is a way to bring up a GUI (jamfhelper, cocoadialog, etc) to give a device an already created EA. As mentioned, the EA is created, I just need a way for a technician who is setting up the computer to assign it to the right EA so that it drops into scope for some policies to run based on the EA. The EA I created has the following characteristics: Data Type: String, Input Type: Pop-Up Menu . I then have a few items in the dropdown to choose. I'm hoping to be able to assign one of those dropdown items directly from the computer.
Extension Attribute - Give a device an EA in GUI dialogue box, dropdown, etc.
Best answer by mm2270
@joethedsa Sure. Here is a script that should do what you're looking for. I already tested this myself so I know it works as long as it's set up correctly.
To use this, you need to use script parameters to pass the API account username, password and the Extension Attribute's id to it.
If you haven't used script parameters before, these are essentially placeholders in the script that, at script run time, the Jamf Pro server will pass down a value for them that you specify in the policy where you add the script. In general, when needing to use things like API account credentials, especially if that account has access to write back to your Jamf server, it's best to put them into parameters so it's more secure. They won't be hardcoded into the script.
The EA ID part isn't as necessary (it could be hardcoded), but I put it there to make it a little more flexible, i.e, if you have other EAs that use drop down values, the same script could be used to provide an interactive way to apply them to a machine.
When using the script in the policy, be sure to add in a value in Parameters 4, 5 and 6, for the API account name, password and the EA's ID (you can view the ID by viewing the Extension Attribute itself - the ID will be in the URL in your browser, like ....computerExtensionAttributes.html?id=8&o=r where 8 is the ID) Make sure the account you add has the ability to read and update computer records.
Here's the script:
#!/bin/bash
APIUSER="$4"
APIPASS="$5"
EA_ID="$6"
## Make sure we got values for the 3 parameters above
if [ -z "$APIUSER" ] || [ -z "$APIPASS" ] || [ -z "$EA_ID" ]; then
echo "One of the required script parameters was not filled in. Please check the policy script parameters and try again."
exit 1
fi
## The Jamf Pro server URL this Mac is enrolled to
JSS_URL=$(/usr/bin/defaults read /Library/Preferences/com.jamfsoftware.jamf.plist jss_url | sed 's|/$||')
## This Mac's UUID/UDID string
UDID=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformUUID/{print $4}')
## The logged in user and their UID
LOGGED_IN_USER=$(stat -f%Su /dev/console)
LOGGED_IN_UID=$(id -u "$LOGGED_IN_USER")
## API call to obtain the Extension Attribute and store in file
curl -H "Accept: text/xml" -ku "${APIUSER}:${APIPASS}" "${JSS_URL}/JSSResource/computerextensionattributes/id/${EA_ID}" | xmllint --format - > /tmp/${EA_ID}.xml
## Get the EA display name
EA_NAME=$(xpath '/computer_extension_attribute/name/text()' < /tmp/${EA_ID}.xml)
## API call to obtain a list of drop down values from the Extension Attribute
VALUES_ALL=$(xpath '/computer_extension_attribute/input_type/popup_choices' < /tmp/${EA_ID}.xml 2>/dev/null | xmllint --format - | awk -F'>|<' '/<choice>/{print $3}')
if [ ! -z "$VALUES_ALL" ]; then
## Values were obtained for the EA. Prompt user to make a selection from a list
SELECTION=$(/bin/launchctl asuser "$LOGGED_IN_UID" sudo -iu "$LOGGED_IN_USER" /usr/bin/osascript << EOD
tell application "System Events"
activate
set Values to do shell script "echo "$VALUES_ALL""
set ValuesList to paragraphs of Values
choose from list ValuesList with prompt "Select a value from the list:"
end tell
EOD)
else
echo "No values obtained in the API"
exit 1
fi
function updateEAValue ()
{
## Create an xml file containing the EA settings
cat << EOF > /tmp/EA_${EA_ID}.xml
<computer>
<extension_attributes>
<extension_attribute>
<id>${EA_ID}</id>
<name>${EA_NAME}</name>
<type>String</type>
<value>${SELECTION}</value>
</extension_attribute>
</extension_attributes>
</computer>
EOF
## Update the current computer's server record with the updated EA value
curl -ku "${APIUSER}:${APIPASS}" "${JSS_URL}/JSSResource/computers/udid/${UDID}" -T "/tmp/EA_${EA_ID}.xml" -X PUT
if [ $? == 0 ]; then
echo "Extension Attribute value updated successfully"
/usr/local/bin/jamf displayMessage -message "Extension Attribute value updated successfully"
rm /tmp/EA_${EA_ID}.xml; rm /tmp/${EA_ID}.xml
exit 0
else
echo "Error when attempting to update the Extension Attribute value"
/usr/local/bin/jamf displayMessage -message "Error when attempting to update the Extension Attribute value"
rm /tmp/EA_${EA_ID}.xml; rm /tmp/${EA_ID}.xml
exit 1
fi
}
if [ "$SELECTION" != "false" ]; then
updateEAValue
else
echo "User canceled from selection. Nothing to do"
exit 0
fi
One other thing - because this has the jamf binary accessing System Events, you might run into a PPPC issue when the script is run on any macOS Mojave systems where they will see a prompt to allow the jamf binary to access System Events. If you deploy a PPPC profile to handle those cases it can bypass it, but if not, the techs will need to "allow" the access before the dialog will appear. Just something to be aware of.
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
