Posted on 04-03-2024 07:22 AM
Is there a way using the API to find the number of devices that has an app installed based on the app ID? I created a script to do it but I went off used VPP license but that takes every device its scoped to into count not the ones with it actually installed which is a much smaller number.
Any help would be great and I will also include the script I created to at least find used VPP licences.
Its rough I know, I am still learning how to script with API requests.
import requests
import json
from datetime import datetime
import xml.etree.ElementTree as ET
import os
import csv
#This is the login for the Bearer Token
username = "ussername"
password = "password"
url = "https://your_jamfcloud_server.jamfcloud.com/"
bearer_token = ""
token_expiration_epoch = 0
#This gets the Bearer Token and makes it usable
def get_bearer_token():
global bearer_token, token_expiration_epoch
response = requests.post(f"{url}/api/v1/auth/token", auth=(username, password))
response_json = response.json()
bearer_token = response_json.get('token', '')
token_expiration = response_json.get('expires', '')
token_expiration_epoch = int(datetime.strptime(token_expiration.split('.')[0], "%Y-%m-%dT%H:%M:%S").timestamp())
directory_path ="path of script and csv of IDs"
# Change the current working directory to the directory containing your CSV file
os.chdir(directory_path)
unsuccessful_ids = []
with open('appID.CSV', 'r') as f:
reader = csv.reader(f)
links = list(reader)
for i in links:
# Using the "match" option in Jamf REST API, find device ID for matching attribute
print("Checking Users For: ", *i)
try:
# This makes the api call.
get_bearer_token()
response = requests.get(f"{url}JSSResource/mobiledeviceapplications/id/"+ str(*i), headers={"Authorization": f"Bearer {bearer_token}"})
headers = {"Accept": "text/xml"}
#This pulls the info out of the xml data
root = ET.fromstring(response.content)
for child in root.iter('used_vpp_licenses'):
print("Current Users: ", child.text)
used_vpp_licenses = child.text
actual_id = i[0]
actual_licenses = child.text
data_to_add = [[actual_id, actual_licenses]]
file_path = 'currentuserbasedonID.csv'
with open(file_path, 'a',newline='') as file:
writer = csv.writer(file)
# Write multiple rows
writer.writerows(data_to_add)
except Exception:
# Ignore the exception and continue
unsuccessful_ids.append(actual_id)
# Print the list of unsuccessful IDs
print("Unsuccessful IDs:", unsuccessful_ids)