Skip to content

Instantly share code, notes, and snippets.

@AndrewWCarson
Created October 30, 2019 17:35
Show Gist options
  • Save AndrewWCarson/adc92125f7e5d856bfcf749519133c95 to your computer and use it in GitHub Desktop.
Save AndrewWCarson/adc92125f7e5d856bfcf749519133c95 to your computer and use it in GitHub Desktop.
Restart All Devices in a Policy
#!/usr/bin/python
# Addigy API Credentials
client_id = 'YOUR ADDIGY API CLIENT_ID HERE'
client_secret = 'YOUR ADDIGY API CLIENT_SECRET HERE'
policy_name = 'YOUR POLICY NAME HERE'
# Libraries
import sys
import requests
import json
# Error & Success Codes for API stuff
HTTPS_SUCCESS=200
REQUEST_ERR = 'API Error: '
# Get all the policies
response = requests.get('https://prod.addigy.com/api/policies', params={'client_id': client_id, 'client_secret': client_secret})
if response.status_code != HTTPS_SUCCESS:
sys.exit(REQUEST_ERR + str(response.status_code))
# Unpack the response to JSON
policies = json.loads(response.text)
print "Found " + str(len(policies)) + " policies."
# Iterate through the policies to find the ID of the one we want.
for policy in policies:
if policy['name'] == policy_name:
policy_id = policy['policyId']
break
print "Policy ID for " + policy_name + " is " + policy_id + "."
# Get all devices in the policy
response = requests.get('https://prod.addigy.com/api/policies/devices', params={'client_id': client_id, 'client_secret': client_secret, 'policy_id': policy_id})
if response.status_code != HTTPS_SUCCESS:
sys.exit(REQUEST_ERR + str(response.status_code))
devices = json.loads(response.text)
print "Found " + str(len(devices)) + " devices."
# Send the restart MDM command to all devices.
for device in devices:
response = requests.get('https://prod.addigy.com/api/mdm/restart', params={'client_id': client_id, 'client_secret': client_secret, 'agent_id': device['agentid']})
if response.status_code == HTTPS_SUCCESS:
print "Successfully sent restart to device: " + device['Device Name'] + "/" + device['agentid']
else:
print "Error sending restart to device: " + device['Device Name'] + "/" + device['agentid']
@AndrewWCarson
Copy link
Author

Obviously, you'll need to edit the first three variables for this to work. Super simple method of getting all devices in a policy and iteratively send the restart MDM to them.

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