Skip to main content
Solved

Script to get users name and set computer name?


Forum|alt.badge.img+8

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!

Best answer by bbelew

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 original
Did this topic help you find an answer to your question?

11 replies

Forum|alt.badge.img+7
  • New Contributor
  • 9 replies
  • June 4, 2015

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.


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • June 4, 2015

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.


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • June 4, 2015

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


Forum|alt.badge.img+12
  • Contributor
  • 222 replies
  • June 4, 2015

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


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • June 4, 2015

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

Forum|alt.badge.img+11
  • Valued Contributor
  • 215 replies
  • June 4, 2015

@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}$')


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • June 4, 2015

@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.


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • Answer
  • June 4, 2015

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

Forum|alt.badge.img+12
  • Valued Contributor
  • 297 replies
  • June 5, 2015

Will this also work for mobile devices?


Forum|alt.badge.img+8
  • Author
  • Contributor
  • 56 replies
  • June 5, 2015

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


Forum|alt.badge.img+1
  • New Contributor
  • 1 reply
  • January 12, 2016

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?


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings