I have been using a Python script that uses a Google Sheet to locate serial numbers and match them up with names. When trying to use this with the M1 Mac's, it seems that the script isn't working as it should. Upon investigation, the script I was using was only updating the computer name, but not the two hostnames. On the Intel Mac's, this didn't seem to be an issue, but with the M1 Mac's, renaming via the script has become unreliable. More often than not, I have to manually rename the computers and manually add them to AD.
Anyone have a better script for this? Here is mine that I cobbled together from other threads on Jamf Nation.
1#!/usr/bin/python2'''3Rename computer from remote CSV using Jamf binary4Pass in the URL to your remote CSV file using script parameter 45The remote CSV could live on a web server you control, OR be a Google Sheet6specified in the following format:7https://docs.google.com/spreadsheets/u/0/d/1H9gsdRtmzb0v9TY8r47j8p8uUs8vhSZS3v446czCOjU/export?format=csv&id=<document ID>&gid=08'''91011import os12import sys13import urllib214import subprocess151617CSV_PATH = '/var/tmp/computernames.csv'181920def download_csv(url):21 '''Downloads a remote CSV file to CSV_PATH'''22 try:23 # open the url24 csv = urllib2.urlopen(url)25 # ensure the local path exists26 directory = os.path.dirname(CSV_PATH)27 if not os.path.exists(directory):28 os.makedirs(directory)29 # write the csv data to the local file30 with open(CSV_PATH, 'w+') as local_file:31 local_file.write(csv.read())32 # return path to local csv file to pass along33 return CSV_PATH34 except (urllib2.HTTPError, urllib2.URLError):35 print 'ERROR: Unable to open URL', url36 return False37 except (IOError, OSError):38 print 'ERROR: Unable to write file at', CSV_PATH39 return False404142def rename_computer(path):43 '''Renames a computer using the Jamf binary and local CSV at <path>'''44 cmd = ['/usr/local/bin/jamf', 'setComputerName', '-fromFile', path]45 proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)46 out, _ = proc.communicate()47 if proc.returncode == 0:48 # on success the jamf binary reports 'Set Computer Name to XXX'49 # so we split the phrase and return the last element50 return out.split(' ')[-1]51 else:52 return False535455def main():56 '''Main'''57 try:58 csv_url = sys.argv[4]59 except ValueError:60 print 'ERROR: You must provide the URL of a remote CSV file.'61 sys.exit(1)62 computernames = download_csv(csv_url)63 if computernames:64 rename = rename_computer(computernames)65 if rename:66 print 'SUCCESS: Set computer name to', rename67 else:68 print ('ERROR: Unable to set computer name. Is this device in the '69 'remote CSV file?')70 sys.exit(1)71 else:72 print 'ERROR: Unable to set computer name without local CSV file.'73 sys.exit(1)747576if __name__ == '__main__':77 main()