Skip to content

Instantly share code, notes, and snippets.

@smithjw
Created April 7, 2022 23:18
Show Gist options
  • Save smithjw/e6eaeb14d2799ac727c05f6d9771d490 to your computer and use it in GitHub Desktop.
Save smithjw/e6eaeb14d2799ac727c05f6d9771d490 to your computer and use it in GitHub Desktop.
Bash JSON Enrolment snippet
#!/bin/bash
echo_logger() {
log_folder="${log_folder:=/private/var/log}"
log_name="${log_name:=log.log}"
mkdir -p $log_folder
echo -e "$(date) - $1" | tee -a $log_folder/$log_name
}
dialog_update() {
echo_logger "DIALOG: $1"
echo "$1" >> "$dialog_command_file"
}
get_json_value() {
JSON="$1" osascript -l 'JavaScript' \
-e 'const env = $.NSProcessInfo.processInfo.environment.objectForKey("JSON").js' \
-e "JSON.parse(env).$2"
}
run_jamf_trigger() {
trigger="$1"
if [ "$trigger" == "recon" ]; then
echo_logger "RUNNING: $jamf_binary $trigger"
"$jamf_binary" "$trigger"
else
echo_logger "RUNNING: $jamf_binary policy -event $trigger"
"$jamf_binary" policy -event "$trigger"
fi
}
policy_array=('
{
"steps": [
{
"listitem": "Installing management tools...",
"icon": "SF=terminal,colour1=#007DBA,colour2=#0064A1,weight=medium",
"trigger_list": [
{
"trigger": "install-Python",
"path": "/usr/local/bin/managed_python3"
},
{
"trigger": "install-Nudge",
"path": "/Applications/Utilities/Nudge.app/Contents/Info.plist"
},
{
"trigger": "install-Outset",
"path": "/usr/local/outset/outset"
}
]
}
]
}
')
for (( i=0; i<dialog_step_length; i++ )); do
# Creating initial variables
listitem=$(get_json_value "${policy_array[*]}" "steps[$i].listitem")
icon=$(get_json_value "${policy_array[*]}" "steps[$i].icon")
trigger_list_length=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list.length")
# If there's a value in the variable, update running swiftDialog
if [[ -n "$listitem" ]]; then dialog_update "listitem: $listitem: wait"; fi
if [[ -n "$icon" ]]; then dialog_update "icon: $icon"; fi
if [[ -n "$trigger_list_length" ]]; then
for (( j=0; j<trigger_list_length; j++ )); do
# Setting variables within the trigger_list
trigger=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list[$j].trigger")
path=$(get_json_value "${policy_array[*]}" "steps[$i].trigger_list[$j].path")
# If the path variable has a value, check if that path exists on disk
if [[ -f "$path" ]]; then
echo_logger "$path exists, moving on"
else
run_jamf_trigger "$trigger"
fi
done
fi
if [[ -n "$listitem" ]]; then dialog_update "listitem: $listitem: ✅"; fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment