Created
November 14, 2024 19:54
-
-
Save vbnin/9a5f2b362b10c3a0b35667b2a89be05c to your computer and use it in GitHub Desktop.
Simple ZSH script showing how to list Jamf Pro departments and create a new department using Jamf Pro API and an API client.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/zsh | |
| # This script will do the following: | |
| # - Generate a bearer token for Jamf Pro | |
| # - List all Jamf Pro departments in the instance | |
| # - Create a new department | |
| # Get Jamf Pro variables | |
| URL="https://YOUR_INSTANCE.jamfcloud.com" | |
| API_CLIENT="" | |
| API_SECRET="" | |
| # New department name | |
| NEW_DEPT="My New Department" | |
| # Generate Bearer Token | |
| tokenCommand=$(curl --request POST \ | |
| --url ${URL}/api/oauth/token \ | |
| --header "Content-Type: application/x-www-form-urlencoded" \ | |
| --data-urlencode "client_id=${API_CLIENT}" \ | |
| --data-urlencode "grant_type=client_credentials" \ | |
| --data-urlencode "client_secret=${API_SECRET}" | |
| ) | |
| bearerToken=$(grep token <<< "$tokenCommand" | cut -d \" -f 4) | |
| # List existing departments | |
| listDeptCommand=$(curl --request GET \ | |
| --silent \ | |
| --url ${URL}/api/v1/departments \ | |
| --header "accept: application/json" \ | |
| --header "Authorization: Bearer $bearerToken" \ | |
| ) | |
| echo "$listDeptCommand" | |
| # Create JSON data | |
| read -r -d '' jsonData <<EOF | |
| { | |
| "name": "$NEW_DEPT" | |
| } | |
| EOF | |
| # Create a new department | |
| createDeptCommand=$(curl --request POST \ | |
| --silent \ | |
| --url ${URL}/api/v1/departments \ | |
| --header "Content-Type: application/json" \ | |
| --header "accept: application/json" \ | |
| --header "Authorization: Bearer $bearerToken" \ | |
| --data "${jsonData}" | |
| ) | |
| echo "$createDeptCommand" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment