Last active
August 31, 2021 14:30
-
-
Save pirkla/9124dcfcd4a6815bbce331670a90ead7 to your computer and use it in GitHub Desktop.
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
import json | |
import requests | |
################# REQUIRED USER DEFINED VARIABLES ############ | |
# set network ID from https://url.jamfcloud.com/devices/enroll.html -> On device enrollment -> Network ID | |
networkId='networkidhere' | |
# set api key from https://url.jamfcloud.com/configuration/api | |
apiKey='apikeyhere' | |
# set base url for instance' | |
baseURL='https://yourdomain.jamfcloud.com/api' | |
# group id array. Can be found in url when viewing group page. For instance: https://test.jamfcloud.com/users/groupDetails/1.html group id is 1 | |
# use the format [2,3,5] | |
groupIds = [9999999,9999999] | |
################ END REQUIRED VARIABLES | |
# set a dict for headers to set api version | |
headers={"X-Server-Protocol-Version":"1"} | |
# send a request to get a list of groups. Basic authorization is handled by requests auth argument | |
groupRequest=requests.get(baseURL +"/users/groups", headers=headers, auth=(networkId,apiKey)) | |
# make sure auth is valid and request worked | |
if (groupRequest.status_code != 200): | |
print("There was an issue with the request, status code: " + str(groupRequest.status_code) + " terminating" ) | |
exit() | |
# use the json module to parse the request into a dict | |
groupJson = json.loads(groupRequest.text) | |
# get the groups value from the dict | |
groupArray = groupJson["groups"] | |
# declare a dictionary and add group id's with names as keys | |
parsedDict=dict() | |
for x in groupArray: | |
parsedDict[x["id"]] = x["name"] | |
# get group names and do sanity check that groups exists | |
groupNames = [] | |
for id in groupIds: | |
name = parsedDict.get(id) | |
if (name == None): | |
print("group with id: " + str(id) + " could not be found. Terminating program") | |
exit() | |
groupNames.append(name) | |
# check with the user if this is correct and we should proceed | |
print("Picked group names are: " + str(groupNames) + " with id:" + str(groupIds)) | |
inputVal=input("Proceed? (Y/n):") | |
if inputVal != "Y": | |
print("You input: " + inputVal + " so I guess we're done here") | |
exit() | |
# send a request to get all users | |
userListRequest = requests.get(baseURL + "/users", headers=headers, auth=(networkId,apiKey)) | |
# load the response into a dict, then get the list of users | |
resultJson = json.loads(userListRequest.text) | |
usersArray = resultJson["users"] | |
deletedUsers = [] | |
# iterate through users and delete each user that is not in one of the groups | |
for user in usersArray: | |
isInGroups = False | |
for id in groupIds: | |
if id in user["groupIds"]: | |
isInGroups = True | |
if not isInGroups: | |
deleteUserRequest = requests.delete(baseURL + "/users/" + str(user["id"]), auth=(networkId,apiKey)) | |
if (deleteUserRequest.status_code != 200): | |
print("There was an issue removing a user: " + str(deleteUserRequest.status_code) + " : " + deleteUserRequest.text) | |
else: | |
print("Removing user " + user["username"]) | |
deletedUsers.append(user["id"]) | |
print("all users not in groups " + str(groupIds) + " with group names " + str(groupNames) + " have been moved to trash") | |
print("user ids moved to trash: " + str(deletedUsers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment