Skip to content

Instantly share code, notes, and snippets.

@haircut
Last active June 27, 2017 14:46
Show Gist options
  • Save haircut/5d1c67491ed879155c3e5c534a9ddcba to your computer and use it in GitHub Desktop.
Save haircut/5d1c67491ed879155c3e5c534a9ddcba to your computer and use it in GitHub Desktop.
Determine the Execution Environment of a Jamf Pro script
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Jamf Pro - Determine Execution Environment
"""
import os
def is_running_directly():
"""
Returns True if script is being run directly from within a policy;
False if script is being run from a policy called by another policy.
"""
if int(os.environ['SHLVL']) >= 2:
return False
else:
return True
def is_running_in_self_service():
"""
Returns True if the execution environment of this script is from within
Self Service, false if otherwise
Thanks to @macmule
https://github.com/dataJAR/jamJAR/blob/master/script/jamJAR.py#L96
"""
if not os.environ.get('USERNAME') and os.environ.get('USER'):
return True
else:
return False
def main():
"""
Main
"""
print os.environ
if is_running_directly():
if is_running_in_self_service():
# Script is running from a policy intiated by a user from within
# Self Service
pass
else:
# Script is running directly in a policy that was initiated by one
# of the "standard" event triggers: Startup, Network State Change,
# or Enrollment Complete
pass
else:
# Script is running in a policy that was called by another policy
# ie. - this script runs in Policy B.
# - Policy A called "jamf policy -id $policy_b_id"
#
# OR
#
# Script is running from a policy triggered by the Login or Logout
# hook
pass
if __name__ == '__main__':
main()
@haircut
Copy link
Author

haircut commented Jun 26, 2017

You wind up with 3 conditions and can perform different actions accordingly.

Conditions should be documented in the gist.

Original use case was to show different verbiage in notifications based on how the policy was called; lots of other uses though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment