Skip to content

Instantly share code, notes, and snippets.

@haircut
Last active December 28, 2020 16:09
Show Gist options
  • Save haircut/035246f794024c6910c468b7278d631a to your computer and use it in GitHub Desktop.
Save haircut/035246f794024c6910c468b7278d631a to your computer and use it in GitHub Desktop.
Running multiple Jamf policies in bash or python; minimal examples
#!/bin/bash
# Policy IDs or custom trigger names
# Bash arrays are specified like the provided example; surround custom triggers with
# quotes, and leave policy ids as "bare" integers
POLICIES=( "custom" "triggers" 523 32 )
for i in "${POLICIES[@]}"; do
# test if array element is an integer, ie. a policy id
if [ "$i" -eq "$i" ] 2>/dev/null
then
/usr/local/bin/jamf policy -id "${i}"
else
/usr/local/bin/jamf policy -event "${i}"
fi
done
#!/usr/bin/python
import subprocess
# Policy IDs or custom trigger names
# Python lists are specified like the provided example; surround custom triggers with
# quotes, and leave policy ids as "bare" integers; delimit with commas
POLICIES = ["custom", "triggers", 523, 32]
def run_jamf_policy(p):
"""Runs a jamf policy by id or event name"""
cmd = ['/usr/local/bin/jamf', 'policy']
if isinstance(p, basestring):
cmd.extend(['-event', p])
elif isinstance(p, int):
cmd.extend(['-id', str(p)])
else:
raise TypeError('Policy identifier must be int or str')
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
result_dict = {
"stdout": out,
"stderr": err,
"status": proc.returncode,
"success": True if proc.returncode == 0 else False
}
return result_dict
def main():
"""Main"""
for policy in POLICIES:
run_jamf_policy(policy)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment