Skip to main content

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.



#!/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/1H9gsdRtmzb0v9TY8r47j8p8uUs8vhSZS3v446czCOjU/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()

You can. I did the echo's so I could follow which step things were on and it helped with troubleshooting.


I am assuming its one of these correct?

echo $JAMF_STATUS
echo $JAMF_MESSAGE

Thank you for this script!


Has anyone else had a problem when exporting the Google Spreadsheet to a CSV?  I went to /var/tmp/ and when I open the csv file, it seems to error out on cell B9 and then stops adding the rest of the records.  The first 6 computers appear, but the computer name appears to always break at cell B9.  I've tried a brand new spreadsheet and moving that record to the bottom of the list, but it error still occurs on B9.


It may be a format error. I had an issue when copying and pasting the info but everything worked when I typed it in.


OK. I figured it out thanks to iTecNote. In Parameter 4, I had to remove the edit#gid=0 from the end of the URL and add export?exportFormat=csv

 

 

Now it's successfully downloading the csv file.  I also am using a completely new Google Spreadsheet.  For some reason, when I went to change the Document ID back to the old one, it would fail again.  I was able to copy and paste all the data into the new one and confirmed the csv gets the correct number of records.


Has anyone had trouble getting this to work recently? I have been using this rename-computer.py for several years now without changing any of the script or policy and all of a sudden it is returning errors. It's acting as if the sheet isn't downloading but the link I'm using downloads the csv file just fine so I'm not sure where the error would be. 

 


Has anyone had trouble getting this to work recently? I have been using this rename-computer.py for several years now without changing any of the script or policy and all of a sudden it is returning errors. It's acting as if the sheet isn't downloading but the link I'm using downloads the csv file just fine so I'm not sure where the error would be. 

 


I tried @dwaterbury 's fix above and am still having the same issue. New sheet, new format, same error.


Has anyone had trouble getting this to work recently? I have been using this rename-computer.py for several years now without changing any of the script or policy and all of a sudden it is returning errors. It's acting as if the sheet isn't downloading but the link I'm using downloads the csv file just fine so I'm not sure where the error would be. 

 


Try using the script I shared above that’s written in bash, instead of python. 


Try using the script I shared above that’s written in bash, instead of python. 


Switching to Bash worked. No idea why Python stopped working all of a sudden today, I use this nearly daily.


Sorry that I wasn't fully clear in my earlier post.  I had switched over to the Bash script because starting around Mac OSX 12.3, it appears Apple has removed Python 2.


Here we are again... Another update another issue (thanks Apple). Still using this script in Bash and it works on Intel, M1 and M2 Macs prior to 2023 models. The script is failing on new M2 2023 MBP 14's.

This is the error its returning but only on these new devices:

"There was an error. You must specify a computer name. Use the -name flag ERROR: Unable to set computer name without local CSV file."

 

I've used this method for years. I have the spreadsheet filled out and the script has been working until today.

 


Here we are again... Another update another issue (thanks Apple). Still using this script in Bash and it works on Intel, M1 and M2 Macs prior to 2023 models. The script is failing on new M2 2023 MBP 14's.

This is the error its returning but only on these new devices:

"There was an error. You must specify a computer name. Use the -name flag ERROR: Unable to set computer name without local CSV file."

 

I've used this method for years. I have the spreadsheet filled out and the script has been working until today.

 


I wonder if the script needs to be written in zsh? How big is the document getting that you have your name file? I know mine is about 1mb, so it downloads pretty quickly. Perhaps yours is too large and the script is running before the csv file is downloaded?


I wonder if the script needs to be written in zsh? How big is the document getting that you have your name file? I know mine is about 1mb, so it downloads pretty quickly. Perhaps yours is too large and the script is running before the csv file is downloaded?


It cant be too big, it's only 160 lines. I currently have a 10 second wait time for download on the script but the error it returns starts off saying that the file downloaded successfully to a /var location.


It cant be too big, it's only 160 lines. I currently have a 10 second wait time for download on the script but the error it returns starts off saying that the file downloaded successfully to a /var location.


Have you tried opening the folder where the CSV is downloading to and watching it to see if the file actually is downloaded? And if it is downloaded, is there any data in it?


Have you tried opening the folder where the CSV is downloading to and watching it to see if the file actually is downloaded? And if it is downloaded, is there any data in it?


Got this working. Wasn't actually an Apple update that killed it but rather a Google update that affected the security/sharing settings on the Google Sheet we used.


Got this working. Wasn't actually an Apple update that killed it but rather a Google update that affected the security/sharing settings on the Google Sheet we used.


That's good to know.


Got this working. Wasn't actually an Apple update that killed it but rather a Google update that affected the security/sharing settings on the Google Sheet we used.


Thanks for the heads up.  I bumped into this issue earlier in the year and made the adjustment without thinking to update the JN thread. :)


Here we are again... Another update another issue (thanks Apple). Still using this script in Bash and it works on Intel, M1 and M2 Macs prior to 2023 models. The script is failing on new M2 2023 MBP 14's.

This is the error its returning but only on these new devices:

"There was an error. You must specify a computer name. Use the -name flag ERROR: Unable to set computer name without local CSV file."

 

I've used this method for years. I have the spreadsheet filled out and the script has been working until today.

 


Hi @kwoodard 

The bash script was working until 10 days ago. Also have been using it for a lonng time.

Please tell me you have a fix...           that would make my day!

Thanks


Hi @kwoodard 

The bash script was working until 10 days ago. Also have been using it for a lonng time.

Please tell me you have a fix...           that would make my day!

Thanks


I'm not sure. Mine is still working.


You mentioned a Google update changed something in Google security, but cannot see what changed. I do have the exact error when the policy runs:  "You must specify a computer name. Use -name flag.

It was working a week ago.


I have the same issue with M2 mac minis.  The same error insisting on -name where this script has -fromFile.


Check the share permissions. I "fixed it" by switching to the following: 

Someone, something rolled it back to Restricted. 


Check the share permissions. I "fixed it" by switching to the following: 

Someone, something rolled it back to Restricted. 


That's still the same on mine.  No joy.  It's still asking for -name


Script result: waiting 10 seconds
finished waiting Successfully downloaded https://drive.google.com/file/d/109bL3ee--------------ijkr-dnu/view?usp=drive_link to /var/tmp/computernames.csv 1 There was an error. You must specify a computer name. Use the -name flag ERROR: Unable to set computer name without local CSV file.


That's still the same on mine.  No joy.  It's still asking for -name


We are only running Airs, but we have both M1 and M2 and they are working just fine.  Be sure you are not using the Python script as that version of Python is no longer functional in Mac OS

 

#!/bin/bash

CSV_PATH='/var/tmp/Tech Inventory based Laptop Naming Sheet - Sheet1.csv'
CSV_URL=$4

# Downloads a remote CSV file to CSV_PATH
function download_csv() {
# -s flag to turn off curl's output
# -f flag to have catchable exit status
# -L for url link
# -o for output to filename
# double quotes around CSV_URL becuase curl prefers the url in quotes
curl -s -f -L -o "$CSV_PATH" ""$CSV_URL""
CURL_STATUS=$?

echo "waiting 10 seconds"
sleep 10
echo "finished waiting"

if [[ $CURL_STATUS -eq 0 ]]; then
echo "Successfully downloaded $CSV_URL to $CSV_PATH"
elif [[ $CURL_STATUS -eq 3 ]]; then
# File I/O error
echo "ERROR: Unable to write file at $CSV_PATH"
exit 1
else
# Download error
echo "ERROR: Unable to open URL at $CSV_URL"
exit 1
fi
}

# Renames a computer using the Jamf binary and local CSV at CSV_PATH
function rename_computer() {
/usr/local/bin/jamf help
JAMF_MESSAGE=$(sudo /usr/local/bin/jamf setComputerName -fromFile "$CSV_PATH")
JAMF_STATUS=$?
echo $JAMF_STATUS
echo $JAMF_MESSAGE
if [[ JAMF_STATUS -eq 0 ]]; then
RENAME=$(echo $JAMF_MESSAGE | awk 'END{print $NF}')
echo $RENAME
if [[ -n RENAME ]]; then
# on success the jamf binary reports 'Set Computer Name to XXX'
# so we split the phrase and return the last element
echo "SUCCESS: Set computer name to $RENAME"
else
echo "ERROR: Unable to set computer name. Is this device in the remote CSV file?"
exit 1
fi
else
echo "ERROR: Unable to set computer name without local CSV file."
exit 1
fi

}

if [[ -z $4 ]]; then
echo 'ERROR: You must provide the URL of a remote CSV file.'
exit 1
fi

CSV_URL=$4

#call download_csv
download_csv

#call rename_computer
rename_computer


Reply