While I'm training customers on how to manage their computers with Jamf | Pro, I'm often asked "How can I rename my computers based on a specific naming convention?" My answer often is that, while we do have several ways to rename computers through the GUI, it's probably not going to fit your needs. However, leveraging the API and a bash script, we can easily achieve what you're looking for.
I'd like to note that, although most admins want a specific naming convention, upon further questioning it becomes clear that with the power of Jamf Pro at their fingertips, they no longer need a strict naming convention for their computers. Let the users name their computers whatever they want, this is a great step for them to feel that they have ownership of their computer. For instance, at Jamf we do not enforce naming conventions for our computers, and mine is called Photography Raptor, which goes well with my old SLR decal I have on the front of my laptop.
However, there are use cases for strict naming conventions for computers. Maybe you're binding to AD, and want to know who has what computer based on the computer name from your AD Server. Setting the hostname can also allow you to see which computers are on the network without having to log into Jamf Pro to check. Whatever the case is, here are some options you have available to you (hint, if you want to just know the best way, skip to Scripting a Solution)
Using the Jamf GUI
If you want to use the Graphical User Interface, your options are going to be somewhat limited. Depending on how you are enrolling your computers, you have different options.
Prestage Imaging: Prestage imaging allows you to define a scope based on serial number or mac address and preconfigure the imaging settings when Casper Imaging is opened. This is only useful if you are using Net Boot or an external Hard Drive to image your computers. If you're imaging your computers with one of these methods, you have three options available: Serial Number, Mac Address, or "List of Names."
Serial Number/Mac Address: This allows you to put a prefix or suffix before the value, but most people want more useful information in the name
List of Names: This may seem tempting, but it's too simplistic to often be useful. You can write out a list of names, separated by a comma. The Jamf Server will dish out the names based on the order the computers are enrolled, so you don't really have any control over which computers get which names.
Target Mode Imaging: Target Mode Imaging (TMI) is when you image one computer from another computer through a thunderbolt cable. If you're imaging If you're utilizing Casper Imaging via TMI you have several more options at your disposal:
Prompt for each computer: Since you're touching each computer anyway with TMI, it's not too big of a step to manually type in the name of the computer, especially if you're putting an asset tag on it. This can be useful, but is obviously time consuming
Use Numerical Order: This can be useful if you're batching groups of computers. It allows you to add a prefix and a suffix, and automatically numbers the computers for you. Just image the computers in the order you want them to be in!
Use Mac Address/Serial: Operates the same as before
Upload CSV File: If you already know what you want your computers name to be based on the serial number, you can upload a CSV file with the serial number in the first column and the desired name in the second column. However, most people don't have a list like that, they are basically handing out their computers at random.
But let's say you're one of the 95% of users NOT imaging computers before you hand them to clients. The only way you can rename computers is by manually going into the computers inventory record, entering a name, and then deploying a policy to reset the computer names AND update the inventory, which is incredibly tedious. So... let's look at some other options.
Using "The MUT"
The MUT (short for Mass Update Tool) is an application developed by a Jamf support rep, Mike Levenick. It's incredibly well documented and has some amazing use cases. It's a simple tool that utilizes our API to make mass changes based on a serial number. So, you upload a CSV file with the Serial Number in one column and the desired name in the next. However, the computers need to be enrolled before this change takes effect, and you need to know exactly what computers are going where before deploying them.
For more information on how to use The MUT, visit http://jssmut.weebly.com/. It's very simple and easy to use, and Mike has provided some amazing documentation.
Scripting a Solution
When it comes to renaming computers, scripting a solution is usually the best way, and you'll see why.
Now, let's say everyone in your organization gets their own work computer, and that upon enrollment, they are logging in with their LDAP credentials. Let's say, for this example, that you want the user's LDAP username and the computer model in the computer name. For example, Randy Johnsmith has a username of rjohns and a 2013 MacBook Air, so his computer should be named:
MBA-rjohns
So, let's break this down. First off, it starts with an abbreviated model. MacBook Pros will be abbreviated MBP, Airs MBA. After that, it has a dash, then the user's username. These are all things we easily have access to through the computer, or through the computer's inventory record.
The Script
So, let's break this script down. This script will be run on the computer that we are renaming, and I'm going to show you how to script it in bash. Before I even start showing you any commands, let's make a list of what we need:
The serial number of the computer
An API call to get all the information in the JSS about the computer
Isolation of the username from the API call
The model of the computer
Abbreviating the model of the computer as MBP or MBA, depending on the model
Renaming the computer
Serial Number:
We will start off by putting the serial number of the computer into a variable we call "serialnumber". This can be achieved through this command:
serialnumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
Using the API to get computer information
We can use the serial number to get the username from the Jamf Pro Server through our API. Now I could spend a good amount of time going into how our API works and what you can do with it, but for this example we are doing a simple GET command to grab information from the computer’s inventory record. Here’s the full command. I’ll use variables to pipe in the unique information:
#!/bin/bash
apiusername='apiusername'
apipassword='password1234'
jssurl='https://mycompany.jamfcloud.com'
xml=$(curl -u ${apiusername}:${apipassword} ${jssurl}/JSSResource/computers/serialnumber/${serialnumber})
Let's break this down a bit:
Curl:
curl is a command used to transfer data to or from the a server. If you run curl https://www.google.com from terminal, you'll get the source code for the homepage of google (this will probably be mostly unintelligible gibberish, but you should at least get an output). We are using curl to access the API for your Jamf Pro server through https://mycompany.jamfcloud.com/JSSResource/.
-u
This is a flag for the curl command that denotes user credentials
${variable}
When you wrap a variable in ${...}, it outputs the value of that variable in its place. That's why you're seeing ${apiusername}, {$apipassword}, ${jssurl} and ${serialnumber} in this script.
/JSSResource/computers/serialnumber/${serialnumber}
This URL is telling us exactly what part of our API to access. More information can be found by putting /api at the end of your Jamf Pro URL (https://mycompany.jamfcloud.com/api). This will GET all the information for that computer in xml form.
xml=$(...)
This puts all the output from our API request into a variable called xml. This allows us to parse through it later to grab the username and model of the computer.
Username & Model
The next step is to use the xml we gathered before to get the username and model of the computer. We can do that by using the xpath command. It will look like this:
username=$(echo ${xml} | xpath -e 'string(/computer/location/username)')
model=$(echo ${xml} | xpath -e 'string(/computer/hardware/model)')
You can easily change the /computer/location/username part of this command to isolate any item within the xml output.
Shortening the Model
The easiest way to shorten the model is through a case statement. I'm not going to go into how a case statement works, I've already spent way too much time explaining curl, and how our API works. It's pretty easy, though, to see what's happening with this command. I'm taking the output of the model variable, which should be MacBook Pro, MacBook, or MacBook Air, depending on the model, and creating a new variable with a shorter name. Any model that doesn't match those three will get the abbreviation UNK, for unknown.
case "model" in
MacBook Pro) short=MBP
;;
MacBook) short=MB
;;
MacBook Air) short=MBA
;;
*) short=UNK
;;
esac
Re-naming the computer
And finally, we have everything we need to rename this computer. First off, lets pump our two variables into another variable called computerName. This will look like this:
computerName=${short}-${username}
Next, lets use this variable to rename the computer using the scutil command:
scutil --set HostName ${computerName}
And while we are at it, lets set the hostname as well:
scutil --set LocalHostName ${computerName}
scutil --set ComputerName ${computerName}
Putting it all together
Now that we've gone over each part, lets look at this whole script in action:
#!/bin/bash
# Hard Coding necessary variables
apiusername='apiusername'
apipassword='password1234'
jssurl='https://mycompany.jamfcloud.com'
# Getting Serial Number of the computer
serialnumber=`system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'`
# Getting the inventory information of the computer in XML form
xml=$(curl -u ${apiusername}:${apipassword} ${jssurl}/JSSResource/computers/serialnumber/${serialnumber})
# Parsing the username and model from xml
username=$(echo ${xml} | xpath -e 'string(/computer/location/username)')
model=$(echo ${xml} | xpath -e 'string(/computer/hardware/model)')
# Abbreviating the model
case "model" in
MacBook Pro) short=MBP
;;
MacBook) short=MB
;;
MacBook Air) short=MBA
;;
*) short=UNK
;;
esac
# Putting the computer's name into the computerName variable
computerName=${short}-${username}
# Renaming the computer, host and local host name
/usr/local/bin/jamf setComputerName -name "${computerName}"
Now that you've created this amazing looking script, lets show you how to deploy it in your environment!
Deploying the Script
Finally, you are ready to deploy this script to your client computers through Jamf Pro! If you have ever deployed a script through Jamf Pro before, this step should be fairly simple. If you haven't, I'll walk you through the steps.
Create the API user account
Add the script to Jamf Pro
Add the script to a policy
Scope the policy to the appropriate computers
Create the API User Account
First off, lets create a user account to access the API. You technically could use your admin account credentials, but since you are typing them in plain text, that is definitely not recommended. Instead, we are going to create an account that has only the credentials it needs to complete the API task.
Go to Settings > JSS User Accounts and Groups > New > Create Standard Account. Name the user something like apiRenameComputer and give it a secure password. Keep the privilege set at custom, then under the privileges window ONLY select Read privileges for computers. Then click Save.
Adding the script to Jamf Pro
In Jamf Pro, go to Settings > Computer Management > Scripts. Then click on New, give the script a name (EG: Renaming Computers), navigate to the Script window, and paste the contents of the script into here. Edit the apiusername, apipassword and jssurl with the correct values. Then click Save.
Adding the script to a Policy
Next, lets create a policy and add our script to it. Navigate to Computers on the top, then Policies on the left, then select New. Give the policy a name, then scroll down to set a trigger of Recurring Check In and a Frequency of Once Per Computer.
Then go to the Script payload on the left side, click Configure in the middle, and add the script from the list.
Lastly, let's scope the policy. Go to the scope window, and either scope it to all computers, or target specific computers and either select a group, a building, or a department by clicking Add.
Next Steps
Awesome work - you've now mastered renaming computers with Jamf Pro! I've added the script we worked on today to my GitHub for your reference, which is complete with error checking and Jamf Parameters, which are both workflows we didn't have time to get into today. You can view the script at https://github.com/schasta218/Rename-Computer.
Comentarios