Skip to main content
Question

Script to set HostName, ect. to Serial# without admin prompt


Forum|alt.badge.img+3
  • New Contributor
  • 7 replies

Hey all, I am looking to create a script that when ran it will grab the Serial Number and set it as the HostName, LocalHostName, & ComputerName. I needed some help with getting it to run without the prompt for an Administrator username and password. So far this is what I have below and I apologize ahead of time I am VERY new to this and trying to learn.

set sys to do shell script "/usr/sbin/system_profiler SPHardwareDataType " without altering line endings

set StringVariable1 to "Serial"

set Serial_Number to do shell script "/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial/ { print $NF }' "


do shell script "scutil --set HostName " & Serial_Number with administrator privileges
do shell script "scutil --set LocalHostName " & Serial_Number with administrator privileges
do shell script "scutil --set ComputerName " & Serial_Number with administrator privileges

display dialog "The Mac has been renamed to " & Serial_Number

16 replies

mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • August 9, 2016

@Kecyre Since renaming a Mac will always require admin privileges, you would need to run a script out of something like Self Service.app, which can escalate its own privs to run root level stuff. There's no way to bypass this admin prompt in a double clickable Applescript, if that's what you were looking to do.

If you decide to go the Self Service route, I would look at doing it in bash rather than trying to use Applescript related commands, since sometimes these fail unless they are really being run by the logged in user. You can still invoke Applescript within a bash script if you need it.


davidacland
Forum|alt.badge.img+18
  • Valued Contributor
  • 1811 replies
  • August 9, 2016

If you're using Casper, you can use a bash script like this:

#!/bin/sh
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`
/usr/sbin/scutil --set ComputerName "${serial}"
/usr/sbin/scutil --set LocalHostName "${serial}"
/usr/sbin/scutil --set HostName "${serial}"
exit 0

Casper will run it as root so won't need admin credentials.

If you need to do it interactively with AppleScript, it is an admin function unfortunately so will need the password to be typed in.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 10, 2016

I guess I don't need to run it in AppleScript. I do not have Casper at the moment. I am just binding the Computers to the domain. I just picked up this task after the Admin left and I was given this task.

Thank you for the info I will try Self Service or see what I can figure out with Bash


mm2270
Forum|alt.badge.img+16
  • Legendary Contributor
  • 7880 replies
  • August 10, 2016

Hi @Kecyre. If you don't have Casper at the moment, then you can't use Self Service. Its part of the Casper Suite.
Do you have something like Apple Remote Desktop? If so, renames can be done there thru the Send Unix command function, selecting to run it as "root". Other than that, not really sure. It requires admin rights to make such a change and there isn't an easy way around that.


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 623 replies
  • August 10, 2016

Do you have a universal administrator account created on your workstations? If so you can bundle all of this up into a Script Editor and export it out as a run-only Application to keep the password safe. If the password is different for each workstation you can do prompts to ask for the admin/password combo (but that is counter to what your post is asking). I have examples of how I've done that if you're interested.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 10, 2016

I do have ARD, I talked management into buying it for me. I have figured out some commands to send and learning more as I go. This site has definitely been a help.


Forum|alt.badge.img+8
  • Contributor
  • 131 replies
  • August 11, 2016

@Kecyre This is the shell script I use.

Load up ARD and click that "UNIX" button and paste the following:

#!/bin/bash 

# Variable
theSerial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')

# Begin script
scutil --set ComputerName "$theSerial"
scutil --set HostName "$theSerial"
scutil --set LocalHostName "$theSerial"
defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName

shutdown -r +1

exit 0

The above does the following:

Gets the serial number, renames all the appropriate areas with the new client name, reboots the computer after 1 min (which I added in for you since my full script towards the bottom is part of a larger imaging configuration so the reboot with it comes WAY later down the line).

My full script is below if you want to play around with it.

#!/bin/bash 

# Name    : setComputerName.sh
# Author  : Nick Smith
# Date    : 20140104
# Purpose : Use the model plus serial number to rename computer.

# Logging
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>>/private/var/log/"$(basename $0)"_"$(date "+%Y%m%d_%H%M%S")".log 2>&1

# Variables
theNow=$(date "+%Y-%m-%d_%H:%M:%S")
theSerial=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
theModel=$(system_profiler SPHardwareDataType | grep 'Model Name:' | awk -F': ' '{print $2;}')

# Begin script
echo "Begin script: $(theNow)"

# Write: ComputerName, HostName, LocalHostName, NetBIOS
touch /private/var/log/fv2.log

if [[ "$theModel" == *Book ]]
    then
    scutil --set ComputerName "D$theSerial"
    scutil --set HostName "D$theSerial"
    scutil --set LocalHostName "D$theSerial"
    defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "D$theSerial"
    echo "D" > /private/var/log/fv2.log
else
    echo "L" > /private/var/log/fv2.log
    scutil --set ComputerName "L$theSerial"
    scutil --set HostName "L$theSerial"
    scutil --set LocalHostName "L$theSerial"
    defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName "L$theSerial"
fi

# Finish up
echo "Finish Script: $theNow"

exit 0

The third line under "# Variables" checks the model number which is used later to see if "*book" is in to represent if the Mac is a Macbook, Macbook Air, Macbook Pro.

This is followed by the "touch" command to create a file in the log directory (touch /private/var/log/fv2.log) so I can determine if the machine is a laptop or desktop and echo out the proper letter representing that. ( L or D ).

Above and below the "else" portion of the script you will see if the machine model is found to be a laptop it echos out "L" to that text file or "D" if it's a desktop.

From Casper (which you could work into just about any management suite or setup if you wanted to) I have a policy that looks in that file and if the letter "L" is found it starts the Filevault 2 encryption. Everyone has their own way of doing it but this is mine and works quite well and thought I would include it just to give you a jump start on managing Mac's if it's new to you.

EDIT - Within ARD you can click the "UNIX" button. Paste in your code and next to "Run command as" click the radio button next to "User" and type in the word root . Then under the Template drop down menu on the top right click "Save as template" and name it as you see fit. This will allow you to have faster access to scripts you will need to use often.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 11, 2016

@Snickasaurus Oh man, thank you so much!

I will work with these to to help get a better feel for it. I really appreciate it. I am hoping one day to talk them into getting us set up with Casper as well.


Forum|alt.badge.img+8
  • Contributor
  • 131 replies
  • August 11, 2016

@Kecyre Sure thing. So is, as of now, ARD the only you have to use for Mac management?


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 11, 2016

@Snickasaurus Yep, that is all I have. Main problem is some users know the local admin logins for the Mac's and have been using them to install all sorts of stuff. This was a fallout from previous IT. I am going to try to clean things up and get it back in check.

I have a bit of a task ahead but I am working on it.


Forum|alt.badge.img+8
  • Contributor
  • 131 replies
  • August 11, 2016

That's rough. I'm home for the next several days. Let me know if you have any other questions or hurdles. I'd be happy to assist.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 12, 2016

@Snickasaurus Is there anyway to remotely trigger an OSX upgrade from Yosemite to El Cap? I know how to remotely push updates through commands but I can't seem to find if there is one for the actual OSX upgrade.


Forum|alt.badge.img+8
  • Contributor
  • 131 replies
  • August 12, 2016

It's not easy without some sort of management server. There is "createOSXinstallPkg" that can take the downloaded OS upgrade app from the app store and packaged it in a way that you can push it from ARD to other machines and have them reboot to perform the upgrade.

Honestly I've only done that with Casper but I'm almost certain you can use it with just plain old ARD but it will take a lot of scripting and testing.

Have you tested your environment to see if El Cap can run without causing any application errors or cause services to stop working? This is the first thing I would do if you have no already.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • 7 replies
  • August 12, 2016

We have a few running it already in the same department. They have had it for months and no issues thus far.


Forum|alt.badge.img+6
  • Contributor
  • 20 replies
  • December 16, 2021

Bumping this old topic. Can anyone confirm that this script still works in Catalina / Big Sur / Monterey, and on T2 / M1 hardware?


scottb
Forum|alt.badge.img+18
  • Valued Contributor
  • 1285 replies
  • December 16, 2021
PhilS wrote:

Bumping this old topic. Can anyone confirm that this script still works in Catalina / Big Sur / Monterey, and on T2 / M1 hardware?


It's pretty easy to test, but I'm just using a simple one-liner for getting the serial into the computer name as that's all we care about.

jamf setComputerName -useSerialNumber

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