Skip to content

Instantly share code, notes, and snippets.

@haircut
Last active February 1, 2023 09:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haircut/1debf91078ce75612bf2f0c3b3d99f03 to your computer and use it in GitHub Desktop.
Save haircut/1debf91078ce75612bf2f0c3b3d99f03 to your computer and use it in GitHub Desktop.
Rename a computer using the Jamf binary and a Google Sheet - full details: https://www.macblog.org/post/automatically-renaming-computers-from-a-google-sheet-with-jamf-pro/
#!/usr/bin/python
'''
Rename computer from remote CSV using Jamf binary
Pass in the URL to your remote CSV file using script parameter 4
The remote CSV could live on a web server you control, OR be a Google Sheet
specified in the following format:
https://docs.google.com/spreadsheets/u/0/d/<document ID>/export?format=csv&id=<document ID>&gid=0
'''
import os
import sys
import urllib2
import subprocess
CSV_PATH = '/var/tmp/computernames.csv'
def download_csv(url):
'''Downloads a remote CSV file to CSV_PATH'''
try:
# open the url
csv = urllib2.urlopen(url)
# ensure the local path exists
directory = os.path.dirname(CSV_PATH)
if not os.path.exists(directory):
os.makedirs(directory)
# write the csv data to the local file
with open(CSV_PATH, 'w+') as local_file:
local_file.write(csv.read())
# return path to local csv file to pass along
return CSV_PATH
except (urllib2.HTTPError, urllib2.URLError):
print 'ERROR: Unable to open URL', url
return False
except (IOError, OSError):
print 'ERROR: Unable to write file at', CSV_PATH
return False
def rename_computer(path):
'''Renames a computer using the Jamf binary and local CSV at <path>'''
cmd = ['/usr/local/bin/jamf', 'setComputerName', '-fromFile', path]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, _ = proc.communicate()
if proc.returncode == 0:
# on success the jamf binary reports 'Set Computer Name to XXX'
# so we split the phrase and return the last element
return out.split(' ')[-1]
else:
return False
def main():
'''Main'''
try:
csv_url = sys.argv[4]
except ValueError:
print 'ERROR: You must provide the URL of a remote CSV file.'
sys.exit(1)
computernames = download_csv(csv_url)
if computernames:
rename = rename_computer(computernames)
if rename:
print 'SUCCESS: Set computer name to', rename
else:
print ('ERROR: Unable to set computer name. Is this device in the '
'remote CSV file?')
sys.exit(1)
else:
print 'ERROR: Unable to set computer name without local CSV file.'
sys.exit(1)
if __name__ == '__main__':
main()
@kolczk
Copy link

kolczk commented Jan 2, 2020

I was so happy to find this. As we are moving from Filewave to Jamf renaming a computer is something I have been struggling with. Have you found a way to have the script look to multiple tabs within a single sheet, or look at multiple Google Sheets? We have 7 schools that we will be migrating and I'm trying to figure out how I can more easily have many people working on this sheet without it becoming too large.

@loicyannou
Copy link

Hello Haircut. First of all, your script is awesome. It's exactly what I was looking for in order to update computer names right after the Jamf enrollment. I'm now trying to make it work with a SharePoint Online link. Unfortunately, Python doesn't seem to open SPO urls. I tested the public link ("anyone with the link can view/download") from a private navigation with success, therefore the link is ok. I Googled this issue with no luck. Your help would be much appreciated. Best

@phredman
Copy link

phredman commented May 27, 2020

@loicyannou - were you able to figure this out? I'm also stuck on this.

I can download the file directly, by appending "&download=1" but the script doesn't seem to know what to do with it still.

@loicyannou
Copy link

@phredman, I was not able to make it work with a SPO public link. My Python skills are very limited.
However, I found out this script: https://github.com/vgrem/Office365-REST-Python-Client
Hopefully, someone will be able to use it in order to make compatible @kolczk's script with SPO.

@SebastianNeyra
Copy link

SebastianNeyra commented Aug 11, 2021

I was able to implement this script within our environment; however, after the names get assigned to a computer, the computer have quotation marks around it. Example: GPurdell-MacBookPro ---> "GPurdell-MacBookPro"
Is there anyway to remove the quotation marks?

@rvtkruys-valtech
Copy link

Hi,

Is there any update available for running this on MacOS 12.3?

@zubair-k
Copy link

I've been looking for an update too. Any luck anyone?

@kolczk
Copy link

kolczk commented Aug 15, 2022 via email

@zubair-k
Copy link

zubair-k commented Aug 15, 2022

Thank you @kolczk,
@rvtkruys-valtech after digging further it's only happening on M1 macs running 12.3+. Try checking the version of Python in /usr/lib.
I only had Python3 in the library which needs to be installed after editing the script to call on Python 3.

@rvtkruys-valtech
Copy link

Hi @zubair-k,

I have used another script, similar to this, but using Bash. The script for renaming computer names are processed during the zero touch deployment (DEP Notify). I didn't had to time yet to research on how to deploy Python3 during the zero touch deployment.

@haircut
Copy link
Author

haircut commented Sep 2, 2022

I've shared a minimal reimplementation of this functionality in ZSH here: https://gist.github.com/haircut/2a409fffe55ba65aa353642e5697f38b

@JZADC
Copy link

JZADC commented Feb 1, 2023

Hello
Is there any update to run this on MacOS 13 upwards? I tried to work with the reimplantation you @haircut posted an the other link but I am not let it work correctly.

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