Jamf Pro API – Creating Categories

As I am continuing on my pursuit to use the Jamf Pro API as much as possible, I started creating scripts that would start to build out my Jamf Pro servers in a quicker, more efficient, and consistent way. I will post these mini scripts here as they come up and hopefully someone else will also find them useful.

For this post within the series, I will be posting about creating categories in Jamf Pro. Each instance that is built out should have a baseline standard of common objects. These objects should include Categories. Categories can help keep your Jamf Pro server organized and place other items (policies in Self Service for example) in a place that makes sense to your users.

Since the same baseline group of categories will be created for each Jamf Pro server that is spun up, we can keep these items in an array for our script to read through and build into our server.

As with all our Jamf Pro API scripts, we will start out with setting our variables.

#!/bin/bash

# Jamf Pro API script to build categories
# Created: 5.23.2022 @robjschroeder

################################
# Variables - edit as needed

# Jamf Pro API Credentials
jamfProAPIUsername="apiUsername"
jamfProAPIPassword="apiPassword"
jamfProURL="https://server.jamfcloud.com"

# Create categories, modify array of categories to be created in Jamf Pro, make sure spaces are escaped
categories=(
	Applications
	Enrollment
	Inventory
	Security
	Software\ Updates
	Productivity
	Testing
)

Now that we’ve built our variables, everything else should be pretty straightforward. We need to get our bearer token for authentication, we need to use the API to create our categories, then we can invalidate our token when we are done. This can all be done with some simple functions. Using functions is a great idea because once you have built a function to do a certain task, like get a bearer token from our Jamf Pro server, you can take that function and apply it to any other script that will be doing the same step so you don’t have to re-write it each time you need to use it. In other cases it helps if you have to call the same function multiple times in the same script. Let’s build our functions for this script.

#
##################################################
# Functions -- no need to edit below

# Get a bearer token for Jamf Pro API Authentication
getBearerToken(){
	# Encode credentials
	encodedCredentials=$( printf "${jamfProAPIUsername}:${jamfProAPIPassword}" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
	
	# Generate an auth token
	authToken=$( /usr/bin/curl "${jamfProURL}/uapi/auth/tokens" \
		--silent \
		--request POST \
		--header "Authorization: Basic ${encodedCredentials}" 
)
	
	# Parse authToken for token, omit expiration
	token=$( /usr/bin/awk -F \" '{ print $4 }' <<< "${authToken}" | /usr/bin/xargs )
}

# Invalidate the token when done
invalidateToken(){
	curl --request POST \
	--url ${jamfProURL}/api/v1/auth/invalidate-token \
	--header 'Accept: application/json' \
	--header "Authorization: Bearer ${token}"
}

# Create category records
createCategories(){
	for category in "${categories[@]}"; do
		curl --request POST \
		--url ${jamfProURL}/api/v1/categories \
		--header 'Accept: application/json' \
		--header 'Content-Type: application/json' \
		--header "Authorization: Bearer ${token}" \
		--data '{"name": "'"${category}"'","priority": 9}'
	done
}

We have our variables, we have our functions, now we just need our script to perform our steps. This is the simplest portion of the script.

#
##################################################
# Script Work
#

getBearerToken
createCategories
invalidateToken

exit 0

Putting it all together our script will look like:

#!/bin/bash

# Jamf Pro API script to build categories
# Created: 5.23.2022 @robjschroeder

################################
# Variables - edit as needed

# Jamf Pro API Credentials
jamfProAPIUsername="apiUsername"
jamfProAPIPassword="apiPassword"
jamfProURL="https://server.jamfcloud.com"

# Create categories, modify array of categories to be created in Jamf Pro, make sure spaces are escaped
categories=(
	Applications
	Enrollment
	Inventory
	Security
	Software\ Updates
	Productivity
	Testing
)

#
##################################################
# Functions -- no need to edit below

# Get a bearer token for Jamf Pro API Authentication
getBearerToken(){
	# Encode credentials
	encodedCredentials=$( printf "${jamfProAPIUsername}:${jamfProAPIPassword}" | /usr/bin/iconv -t ISO-8859-1 | /usr/bin/base64 -i - )
	
	# Generate an auth token
	authToken=$( /usr/bin/curl "${jamfProURL}/uapi/auth/tokens" \
		--silent \
		--request POST \
		--header "Authorization: Basic ${encodedCredentials}" 
)
	
	# Parse authToken for token, omit expiration
	token=$( /usr/bin/awk -F \" '{ print $4 }' <<< "${authToken}" | /usr/bin/xargs )
}

# Invalidate the token when done
invalidateToken(){
	curl --request POST \
	--url ${jamfProURL}/api/v1/auth/invalidate-token \
	--header 'Accept: application/json' \
	--header "Authorization: Bearer ${token}"
}

# Create category records
createCategories(){
	for category in "${categories[@]}"; do
		curl --request POST \
		--url ${jamfProURL}/api/v1/categories \
		--header 'Accept: application/json' \
		--header 'Content-Type: application/json' \
		--header "Authorization: Bearer ${token}" \
		--data '{"name": "'"${category}"'","priority": 9}'
	done
}

#
##################################################
# Script Work
#

getBearerToken
createCategories
invalidateToken

exit 0

https://github.com/robjschroeder/Jamf-API-Scripts/blob/main/api-CreateCategories.sh

Thanks for checking it out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: