Script to get users name and set computer name?

bbelew
Contributor

Just curious if it is possible and if anyone has a script that queries JSS for the assigned users name and names the computer to match?

I'm trying to set my workflow hands off. The users will go through the setup process and create their own accounts. While i'll try to steer them in the right direction. There is no guarantee they won't get crafty and put something different for the full name or username. Since my content filter reports based on username and computer name, I wanted at least one of those to be something I control.

Thanks!

1 ACCEPTED SOLUTION

bbelew
Contributor

Working script.

#!/bin/sh

jssUser=apiuser
jssPass='apipassword'

serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

response=$(curl -k https://jss.domain.org:8443/JSSResource/computers/serialnumber/${serial}/subset/location --user "${jssUser}:${jssPass}")
real_name=$(echo $response | /usr/bin/awk -F'<real_name>|</real_name>' '{print $2}');
user_name=$(echo $response | /usr/bin/awk -F'<username>|</username>' '{print $2}');

/usr/sbin/scutil --set ComputerName "${real_name}"
/usr/sbin/scutil --set LocalHostName ${user_name}
dscacheutil -flushcache

View solution in original post

11 REPLIES 11

cchichester
New Contributor III

You could try something like this.

#!/bin/sh
export LOCALMACHINENAME=$(ls -l /dev/console | cut -d " " -f4)
/usr/sbin/networksetup -setcomputername "$LOCALMACHINENAME"

It should name the computer to match the username of whoever is logged in when it runs.

bbelew
Contributor

The username is my fall back. I found a script earlier that could do that. If we were using a directory for authentication that would be the easiest way to do it. With our roll-out though it is pretty much completely un-tethered, all local logins with no directory binding. So the students can technically create whatever they want for a username when they setup the devices. I'll have a printout for them with step by step instructions with their username and passwords so they can mirror their SIS and Canvas credentials but there is nothing stopping them from typing something different.

bbelew
Contributor

It looks like I can use the API, search by serial number, parse the json and return the username record.

Simmo
Contributor II
Contributor II

@bbelew If you write something for it, I'd be interested in seeing it.

bbelew
Contributor

Completely untested but I think this will work. I'll test it when I get to a computer that is tied to JSS. It needs a conditional added to it. If real_name is blank, set computer name to serial number or something. Assuming the script runs before a user is associated with the computer in JSS it would return blank. Mac OS probably won't like a blank computer name and hostname.

#!/bin/sh

jssUser=apiusername
jssPass='apipassword'
jssHost=https://jss.address.org:8443/

serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')


response=$(curl -k ${jssHost}/JSSResource/computers/serialnumber/${serial}/subset/location --user "${jssUser}:${jssPass}")
real_name=$(echo $response | /usr/bin/awk -F'<real_name>|</real_name>' '{print $2}');

/usr/sbin/scutil --set ComputerName $real_name
/usr/sbin/scutil --set HostName $real_name

dmw3
Contributor III

@bbelew You might want to shorten the serial number that returns, especially if in the future the computer gets bound to AD as there is a limit in the number of characters that can be used in a name (15).

We use this to extract the last seven characters of the serial number:

lastCharsInSerialNum=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | grep -o '.{7}$')

bbelew
Contributor

@dmw3 The retrieval of the serial is just used to query the API to return the Real_Name of the user record associated with the computer in JSS. Now Real_Name could be over 15 characters, so it might need to be trimmed. Or i'd just modify the script to pull username instead. Which at least in my environment i've never seen one 15 characters long. My reasoning for wanting the script is mostly for our content filter. So I can see who is searching without requiring them to sign into the filter or having to search JSS to find the username associated with a SN or Mac Address.

bbelew
Contributor

Working script.

#!/bin/sh

jssUser=apiuser
jssPass='apipassword'

serial=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')

response=$(curl -k https://jss.domain.org:8443/JSSResource/computers/serialnumber/${serial}/subset/location --user "${jssUser}:${jssPass}")
real_name=$(echo $response | /usr/bin/awk -F'<real_name>|</real_name>' '{print $2}');
user_name=$(echo $response | /usr/bin/awk -F'<username>|</username>' '{print $2}');

/usr/sbin/scutil --set ComputerName "${real_name}"
/usr/sbin/scutil --set LocalHostName ${user_name}
dscacheutil -flushcache

View solution in original post

St0rMl0rD
Contributor III

Will this also work for mobile devices?

bbelew
Contributor

No. iOS devices running iOS8 you can name, and force name from JSS.

mjmurillo
New Contributor

I get the following output as the computer name

?xml version="1.0" encoding="UTF-8"?><computer><location><username>mmurillo</username><real_name/><email_address/><position/><phone/><department/>

How do I just extract the user name?