Skip to content

Instantly share code, notes, and snippets.

@haircut
Last active July 31, 2018 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haircut/393da69bd72fb45850680d46e81aca5a to your computer and use it in GitHub Desktop.
Save haircut/393da69bd72fb45850680d46e81aca5a to your computer and use it in GitHub Desktop.
Utility function to run a Jamf Pro policy
# encoding: utf-8
import subprocess
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()
# Check for failures that don't affect the return code
failure_indicators = ['No policies were found',
'Script exit code: 1',
'not available on the HTTP server',
'No such file or directory',
]
if any(indicator in out for indicator in failure_indicators):
success = False
else:
success = True if proc.returncode == 0 else False
result_dict = {
"stdout": out,
"stderr": err,
"status": proc.returncode,
"success": success
}
return result_dict
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment