Skip to main content
Jamf Nation, hosted by Jamf, is a knowledgeable community of Apple-focused admins and Jamf users. If you like what you see, join us in person at the ninth annual Jamf Nation User Conference (JNUC) this October for three days of learning, laughter and IT love.

API Assistance Needed for mobile_device update

Hi All,

So I could use some assistance... I'm trying to create a script that updates the user record on an ipad. That is... On the mobile device, I can update the username the ipad is associated to. The goal is to basically have a script that re-assigns an ipad to a different user based off of the csv file. The problem i'm running into, is instead of it changing to the new user record. It OVERWRITES the old user record with the new user's information. Effectively destroying the old record which is still in use.

Now, i'm not the best scripter, and i'm almost there -- I just need to figure out this last part.
I'm essentially trying to mimick the MUT: Mobile Device/Username attribute.

Do I use POST? or PUT? can i do so using the username attribute? or do i need to use the user's "ID" attribute?
Pay attention to this line specifically:

putXML = '<mobile_device><general><display_name>'stu_id'</display_name><device_name>'stu_id'</device_name><name>'stu_id'</name></general><location><username>'stu_id'</username></location></mobile_device>'

Please let me know, Thank you!
Code:

import requests import csv import sys import datetime

sess = requests.Session()

adapter = requests.adapters.HTTPAdapter(max_retries = 20) sess.mount('https://rsf.jamfcloud.com', adapter) username = '' password = '' jss_api_base_url='https://rsf.jamfcloud.com/JSSResource/users/name/' jss_api_base_url2='https://rsf.jamfcloud.com/JSSResource/mobiledevices/serialnumber/'

verifySSL = True

requestHeaders = { 'Content-Type': 'application/xml', 'Accept': 'application/xml', } with open('JamfExportStudent.csv') as csvfile: spamreader = csv.reader(csvfile,delimiter=',') for row in spamreader: sn_id = row[0] stu_id = row[1] stu_gr = row[5] stu_fn = row[2] stu_mn = row[3] stu_ln = row[4] pat_typ = row[6] Staff= "Staff" if pat_typ == "Student": putStr = jss_api_base_url stu_id putXML = '<user><full_name>'+stu_fn' 'stu_mn' 'stu_ln'</full_name><position>'stu_gr'</position><email_address>'stu_id'@rsf.k12.ca.us</email_address></user>' request = sess.put(putStr,putXML,headers=requestHeaders, verify=verifySSL, auth=(username, password)) if request.status_code == 201: successMsg = 'Update Student - SUCCESS! User:'stu_id' Name: 'stu_fn' 'stu_mn' 'stu_ln print(successMsg) elif request.status_code == 404: errorMsg = 'Update ERROR! User:'+stu_id' Name: 'stu_fn' 'stu_mn' 'stu_ln print(errorMsg) putStr = jss_api_base_url2 sn_id putXML = '<mobile_device><general><display_name>'stu_id'</display_name><device_name>'stu_id'</device_name><name>'stu_id'</name></general><location><username>'stu_id'</username></location></mobile_device>' requestDevice = sess.put(putStr,putXML,headers=requestHeaders, verify=verifySSL, auth=(username, password)) if requestDevice.status_code == 201: successMsg = 'Update iPad - SUCCESS! User:'stu_id' Serial: 'sn_id print(successMsg) elif requestDevice.status_code == 404: errorMsg = 'Update iPad - ERROR! User:'+stu_id' Serial: 'sn_id print(errorMsg) elif pat_typ == "Staff": putStr = jss_api_base_url stu_fn '.'+stu_ln putXML = '<user><full_name>'+stu_fn' 'stu_mn' 'stu_ln'</full_name><position>Staff</position><email_address>'stu_fn'.'stu_ln'@rsf.k12.ca.us</email_address></user>' requestDevice = sess.put(putStr,putXML,headers=requestHeaders, verify=verifySSL, auth=(username, password)) if requestDevice.status_code == 201: successMsg = 'Update Staff - SUCCESS! ' stu_fn "."stu_ln print(successMsg) elif requestDevice.status_code == 404: errorMsg = 'Update Staff - ERROR!' stu_fn "."+stu_ln print(errorMsg) putStr = jss_api_base_url2 sn_id putXML = '<mobile_device><general><display_name>'stu_fn'.'stu_ln'</display_name><device_name>'stu_fn'.'stu_ln'</device_name><name>'stu_fn'.'stu_ln'</name></general><location><username>'stu_fn'.'stu_ln'</username></location></mobile_device>' requestDevice = sess.put(putStr,putXML,headers=requestHeaders, verify=verifySSL, auth=(username, password)) if requestDevice.status_code == 201: successMsg = 'Update iPad Staff - SUCCESS! User:'stu_id' Serial: 'sn_id print(successMsg) elif requestDevice.status_code == 404: errorMsg = 'Update iPad Staff - ERROR! User:'+stu_id' Serial: '+sn_id print(errorMsg) print("Upload Complete:") , datetime.datetime.now()
Like Comment