Last active
July 31, 2018 02:25
-
-
Save haircut/393da69bd72fb45850680d46e81aca5a to your computer and use it in GitHub Desktop.
Utility function to run a Jamf Pro policy
This file contains 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
# 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