Posted on 05-18-2015 12:22 PM
HI,
Does anyone have a working script to add asset numbers to iPad records using a csv file?
We want to scan serial number and asset tag at 1 to 1 rollout, and then upload to put asset number into the iPad record.
Thanks!
Sandy
Solved! Go to Solution.
Posted on 05-18-2015 01:39 PM
Hi @Sandy
I hope you are doing well. Support has recently come up with and in-house python script that could potentially complete this for you. I recommend you reach out to your technical account manager and we can set something up to look at your environment and see if the script we have on hand will get your asset tags imported to your devices.
Best,
Christian Dooley
JAMF Support
Posted on 05-18-2015 12:38 PM
I have a PowerShell script that does this, I don't think it will translate well to bash but might be transferable to Python. Would that help you?
Posted on 08-14-2024 08:35 AM
I am needing to do this as well. Do you mind sharing the PowerShell script, I prefer PS. Thanks!
Posted on 05-18-2015 12:40 PM
Python I think would work.
Thanks, Brandon
Posted on 05-18-2015 12:42 PM
I'll see if I can translate it over to Python then. Might take a day though, especially since I am still fairly new to Python
Posted on 05-18-2015 12:47 PM
wow, thanks!
Posted on 05-18-2015 01:39 PM
Hi @Sandy
I hope you are doing well. Support has recently come up with and in-house python script that could potentially complete this for you. I recommend you reach out to your technical account manager and we can set something up to look at your environment and see if the script we have on hand will get your asset tags imported to your devices.
Best,
Christian Dooley
JAMF Support
Posted on 05-18-2015 01:57 PM
Did you catch that @usher.br ?
I have a script from my TAM
Don't want you to use your time on this :)
Thanks!
Sandy
Posted on 05-18-2015 02:03 PM
Well, it didn't take me as long as I thought it would.
I tested this with all 3,000 and change devices in my current environment.
There is only 1 requirement for this script to run. Python Requests. You can install it via either of the following ways listed here
After that is done, simply edit the Python file and change the URL, Username and Password. Then open a terminal window and follow the below steps. Note: I am assuming the file is stored on the desktop:
cd ~/Desktop
python update_assettags.py ~/asset_tags.csv
As far as the csv goes, simply put the following headers at the top of their respective columns "serial_number,asset_tag"
That's it. Below is the actual script. Good luck!
##
# This script was created by Brandon Usher on 2015/05/18
# This script may be distributed any way seen fit, so long as it remains free
# If you paid money for this script, immediately ask for a refund
##
import requests, csv, sys
# Location of the file that contains serial_number and asset_tag columns
importFile = sys.argv[1]
# Set this to False if you have any errors
verifySSL = False
# Set JSS Variables here
# JSS URL must end with / like so:
# https://jss.example.com:8443/
jssURL = 'https://jss.example.com:8443/'
jssAPIUsername = 'username'
jssAPIPassword = 'password'
## ##
# DO NOT CHANGE ANY OF THE BELOW #
## ##
requestHeaders = {
'Content-Type': 'application/xml',
'Accept': 'application/xml',
}
try:
reader = csv.DictReader(open(importFile), delimiter=',', quotechar='"')
except Exception as e:
print(e)
sys.exit(0)
apiEndpoint = jssURL + "JSSResource/mobiledevices"
# Simply loop through all the records in the CSV, minus the header
# since the reader sees the first line as the reference point
for row in reader:
import_values = {}
try:
# This will loop through every column and import the values into the
# import_values variable where we can reference it much easier
for col in row:
value = row[col]
import_values[col] = str(value)
# Now once all the columns are imported into import_values we can use the API
# to update the device records
# Data to be sent to server
xmlData = """<?xml version='1.0' encoding='utf-8'?>
<mobile_device>
<general>
<asset_tag>%s</asset_tag>
</general>
</mobile_device>
""" % (import_values['asset_tag'])
deviceURL = '%s/serialnumber/%s' % (apiEndpoint, import_values['serial_number'])
# This is where the request is sent to update the asset tag of the device
request = requests.put(deviceURL, data=xmlData, headers=requestHeaders, verify=verifySSL, auth=(jssAPIUsername, jssAPIPassword))
#request = requests.get(deviceURL, auth=(jssAPIUsername, jssAPIPassword), verify=False)
if request.status_code == 201:
successMsg = 'Updated device %s with asset tag %s' % (import_values['serial_number'], import_values['asset_tag'])
print(successMsg)
elif request.status_code == 404:
errorMsg = 'Device %s with asset tag %s was not found in the JSS' % (import_values['serial_number'], import_values['asset_tag'])
print(errorMsg)
else:
raise Exception(
request.text
)
except Exception as e:
print(e)
print('Finished importing devices')
I just saw the post about JAMF having a script they created, but I still went through writing this as it gives me more practice with Python
Posted on 05-18-2015 05:22 PM
Just now seeing this thread. I've got a shell script version to do this too.
Posted on 05-18-2015 05:50 PM
Also wish I had seen this earlier.
For our 1:1 roll out, we built a simple web app that used PHP cURL to update the JSS as the laptops were processed. This allowed us to use the bar code readers and not have to keep up with a spreadsheet (or 12).
Both @talkingmoose and @usher.br methods are very similar to what we did. Glad that you found a solution that will work for you.
Posted on 05-21-2015 07:39 AM
@usher.br this looks awesome! I'm on a mac running yosemite, got requests installed just fine, ran the script on a test csv (Saved as windows formatted in mac excel, so that's playing nice), and it'll spit back out:
python update_assettags.py asset_tags.csv
'asset_tag'
Finished importing devices
When I go to JSS it didn't seem to update. Looking at the code I should be getting a 201 response with the details of the serial and asset tag right? Anything you can think of that I might be missing?
When I build the API url manually using the format you have in there it shows as
<mobile_device>
<general>
<id>2116</id>
<display_name>iPad</display_name>
<device_name>iPad</device_name>
<name>iPad</name>
<asset_tag/>
<last_inventory_update>Wednesday, May 20 2015 at 11:54 AM</last_inventory_update>
I even turned on debug logging to see if I could see the API user hitting the logs trying to write some of the xml data but it didn't show up there either.
Any ideas?
Posted on 05-21-2015 07:50 AM
@stoneacheck Yes, what's your CSV header look like? It needs to be serial_number and asset_tag like the following:
serial_number,asset_tag
DYTJ47UUDJ8T,58375
DMPHMWD6DJ8T,57722
I also ran into that issue when I saved the CSV via Excel because it was doing tab delimited instead of comma delimited. Open the file in a text editor and confirm it is indeed comma delimited
Posted on 05-21-2015 07:52 AM
Posted on 05-21-2015 08:37 AM
@stoneacheck @usher.br Make sure the CSV file ends with a hard return/blank line.
I've seen issues with looping through CSV's if that isn't in there.
Posted on 05-21-2015 09:02 AM
I did the extra blank line trick, which still didn't work on my mac. I think it has to do with the SSL/urllib3 inside of the requests part of python, given that my iMac has python 2.7.6 and it's probably mad about poodle.
Since I didn't feel like reading through all the stuff to upgrade python to play nice I just ran it in my Ubuntu vm and it worked just beautifully. Thanks for the script!
Posted on 05-21-2015 12:21 PM
whoa, that was a long path.... not feeling very smart this week
This is my very first foray into Python, and using Mac w/ 10.10.3
using Python 2.7.2 I could not get past the ssl warnings, said finished importing but no data was added to record
sooo..
Installed Python 3.4.3 (which is now a .pkg, installs into Applications, includes pip by default and also has Python Launcher app)
Installed Requests for python 3 using: pip3 install requests (which i had already downloaded)
cd ~/Desktop
python3 update_assettags.py ~/Desktop/asset_tags.csv
Brandon's script worked! Hallelujah
also
updated pip to newest using pip3 install --upgrade pip
This looks so simple now that it's done LOL!
When my head stops spinning I may re-visit the other options....
Thanks everyone!
Sandy
Posted on 05-21-2015 12:38 PM
That csv looks fine to me @stoneacheck but I'm glad you got it working with a Ubuntu box. No idea why it would differ unless Python 2 vs Python 3 process csv reading differently.
@Sandy The reason it said it was done even if nothing happened is because I didn't put any logic at the end of the script to check for errors. I just simply outputted the finished message no matter what. Was mainly used for knowing the script finished. Glad to hear it worked for you as well!
In general, I am thinking of converting all of my JSS API scripts into Python scripts and uploading them to GitHub for easy sharing and cross platform compatibility, especially since all my current scripts are written in PowerShell
Edit: I started a GitHub Repo for my script conversion to Python: https://github.com/brandonusher/JSS-API/
Posted on 04-08-2016 07:20 AM
@brandonusher Brandon.
Thanks for this post.
The script is working correctly.
For new users to Python you will need to do the following to get this to work.
1. Install the latest Python version ( https://www.python.org/downloads/)
2. Install PIP and Requests by following this video (https://www.youtube.com/watch?v=yBdZZGPpYxg&nohtml5=False)
Karl Senger