Posted on 10-19-2021 11:50 AM
Does anyone have a script that will name a Mac, and then keep the computer name and host name synced up? We typically name our Macs as app-serialnumber. The "app-" designates that it is an Apple, but the serial number is just that. Any help?
Solved! Go to Solution.
Posted on 10-20-2021 04:42 PM
Here is a script that should do exactly what you asked. If the computer is not named appropriately it fixes it and updates JAMF to make sure the name is proper. If the name matches it does nothing.
You could use this with OutSet in the boot-every section so that each time the machine is started it would make this check and correct it if necessary.
#!/bin/bash
# Setup Variables
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F'"' '/IOPlatformSerialNumber/{print $(NF-1)}')
desiredComputerName="app-$serialNumber"
#The next block will check the current computer name and change it if it doesn't match desiredComputerName.
if [ "$desiredComputerName" != "$HOSTNAME" ]; then
/usr/local/jamf/bin/jamf setcomputername -name "$desiredComputerName"
/usr/local/jamf/bin/jamf recon
fi
10-19-2021 12:33 PM - edited 10-19-2021 12:33 PM
We have a script we use that has been working well for a number of years. It is a modified version of the response in this post from Freddie Cox. Sets the name of the device based on input and then we run a recon at the end of the process. This will set the name of the device in both spots with little fuss.
Re: Get and Rename Computer Name Interactively - Jamf Nation Community - 99267
Posted on 10-19-2021 12:58 PM
When we first started using JAMF we found this python script in jamfNATION and it has worked perfectly. We do as the author suggests and just maintain a Google sheet with column A containing the serial numbers of our machines and column B containing the name. Because this will download as a CSV is it important to keep the Google sheet sorted by column A, serial number. The columns do not have a header row, it is just serial number and machine name starting in row 1.
We link the script to a policy that runs on enrollment and once a week at check-in.
If a computer's designation gets changed we just edit this sheet and wait for the policy to run. Within a week the computer gets its new name.
The only change we made to the script was in line 58, rather than pulling the URL for the .csv file from an argument passed to the script, we just hard code the URL in the csv_url variable declaration.
#!/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>
'''
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/jamf/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()
Posted on 11-09-2022 10:59 PM
Does this work only for Jamf MDM?
Posted on 10-19-2021 01:37 PM
Here is a very basic script we use, but I plan to take the information learned in @talkingmoose 's JNUC 2021 session (#1104) presented this morning to completely change the process by which we collect the parameters used when naming computers.
Quick FYI -- the command "jamf setComputerName...." performs two of the three "scutil" commands normally performed in a name change.
This script looks at the current Building, grabs the current user and the device serial number, and mashes them together. The script relies on the fact that Parameter 3 ($3) is the "Currently Logged In User" by default. The building is specified in a policy that runs the script (again, we are going to revamp our method of collecting and using machine data).
#######################################################################################
#
# Script to rename mobile computers in the Jamf framework using the following criteria:
# BLD-username-SERIALNUMBER
#
# Must set parameter 4 as "Building Code" for this script to work as desired
#
#######################################################################################
# Set building code
# Building Code assigned in "Jamf policy settings >> Scripts" with Parameter 4
# Use "jamf" command to set the computer name
jamf setComputerName -useSerialNumber -prefix $4"-"$3"-"
#Assign the computer name
computerName=$( /usr/sbin/scutil --get ComputerName )
scutil --set HostName $computerName
Posted on 10-20-2021 04:42 PM
Here is a script that should do exactly what you asked. If the computer is not named appropriately it fixes it and updates JAMF to make sure the name is proper. If the name matches it does nothing.
You could use this with OutSet in the boot-every section so that each time the machine is started it would make this check and correct it if necessary.
#!/bin/bash
# Setup Variables
serialNumber=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F'"' '/IOPlatformSerialNumber/{print $(NF-1)}')
desiredComputerName="app-$serialNumber"
#The next block will check the current computer name and change it if it doesn't match desiredComputerName.
if [ "$desiredComputerName" != "$HOSTNAME" ]; then
/usr/local/jamf/bin/jamf setcomputername -name "$desiredComputerName"
/usr/local/jamf/bin/jamf recon
fi
Posted on 10-21-2021 07:25 AM
Wow that is fantastic, thank you! I will try this in the next day or so.
Posted on 10-21-2021 03:00 PM
This did exactly what I needed, so thank you!