Change ComputerName script

zmbarker
Contributor

I need some help with this script.

AppleScript that scutil --get ComputerName and then asks to check to see if we need to change the ComputerName
Everything works fine As Long As there is a ComputerName to get. Meaning the Computer Name is already set to something.

However, if the Computer Name is not already set then when the script runs the scutil --get ComputerName I receive the error: "ComputerName is not set" and the script fails.

How do I get past the "not set" error?

-- ---------------------------
set localHostName to ""
set hostName to ""
set CurrentComputerName to do shell script "scutil --get ComputerName"
set CompName to ""

if (CurrentComputerName is "not set") or (CurrentComputerName is "") or (CompName is "not set") or (CompName is "") then set DeviceNumberDialog to display dialog "Current Computer Name is: " & (CurrentComputerName) & return & return & "Do you want to change the Computer Name?" & return & return & "Please enter the device number of this computer." & return & return buttons {"Keep", "Change"} default answer "" with title "Enter Device Number" default button "Change"

if text returned of DeviceNumberDialog is equal to "Why" or text returned of DeviceNumberDialog is equal to "WhyME" then set confirmDeviceNumber to display dialog "The current Computer name is " & (CurrentComputerName) & return & return & "Are you sure you want to set this computer name to" & text returned of DeviceNumberDialog & ""?" & return & return buttons {"No", "Yes"} with title "Enter Device Number" default button "No" if button returned of confirmDeviceNumber is "Yes" then set CompName to text returned of DeviceNumberDialog end if end if

if (CompName is not "") then do shell script "scutil --get ComputerName"

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

display dialog "Computer Name has been changed to: " & CompName end if
end if
-- ---------------------------

8 REPLIES 8

PeterClarke
Contributor II

Hi just an aside:

You do know that there is a feature already in Casper for doing this ?

Policy Advanced, Reset Computer Names.

That said, I can also think of reasons why you might want to script it.

zmbarker
Contributor

I am aware of that feature, however the feature Resets ComputerName to the name that is in the JSS. If the JSS does not have the correct name for the computer I do think this feature will work.

ernstcs
Contributor III

What I believe several do (like I do), mainly at the time of imaging, is they create a file somewhere on the box that contains the name of the computer when deployed. This is done in many different ways of course, but typically a script can do it At Reboot and pull it from a variety of locations. Then when you wish to enforce the naming of a computer based on what it was originally named your script references your computer name file.

There was a particular problem in 10.8 where machines were not naming at imaging properly, and again depending upon how you wish to get the name you wanted, different approaches. More information about that located in my other thread.

https://jamfnation.jamfsoftware.com/discussion.html?id=5078

barnesaw
Contributor III

We just rename the machine in the JSS, then run the reset computer name.

jbrimager
New Contributor

I am working on a script to rename systems during imaging. I have Looked all through the forums but havent been able to find exactly what I need. I am a linux guy having this thrust upon him btw, SO right now I can have it rename using my prefix and serial of the system. I am trying to figure out how to make it use only part of the serial. say the last 6 numbers. can't figure out the right syntax. SO far this works

#!/bin/bash

base="***LMAC-"
tld="***"
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print $NF}'`

/usr/sbin/scutil --set ComputerName $base$serial
/usr/sbin/scutil --set LocalHostName $base$serial
/usr/sbin/scutil --set HostName $base$serial.tld

any thoughts on how to pull just the section of the serial string I need?

jbrimager
New Contributor

Figured this out on my own pretty quick. What I'd like to do from here is set it up where I set the parameters to fill in the *** before lmac with one of mite site prefixes so NRHLMAC
LEWLMAC and so on

#!/bin/bash

base="***LMAC"
tld="***.com"
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print substr($0,length-5)}'`

/usr/sbin/scutil --set ComputerName $base$serial
/usr/sbin/scutil --set LocalHostName $base$serial
/usr/sbin/scutil --set HostName $base$serial.tld

jbrimager
New Contributor

Okay so, I figured this out too but, when I run this and then a Bind to AD it runs too quickly and never sends the message to jss that is has run the policy.and the pc binds with the wrong name. Guess I need to dig some more.

#!/bin/bash

base=$4
tld="domain.com"
serial=`/usr/sbin/system_profiler SPHardwareDataType | /usr/bin/awk '/Serial Number (system)/ {print substr($0,length-5)}'`

/usr/sbin/scutil --set ComputerName $base$serial
/usr/sbin/scutil --set LocalHostName $base$serial
/usr/sbin/scutil --set HostName $base$serial.tld

tkimpton
Valued Contributor II

@jbrimager

dude.. like ernstcs said. Check out the link, see my my method.

Will save you a lot if hassle.