Hello,
I am attempting to rename about 300 macs and was just curious if there was a way to do this…
What I want to do is have a script find the serial number of a machine, find that number in a text file, then match it to a PIN that was assigned to that machine, and then name it that. Here is what I have so far…
osascript -e ‘set SN to last word of paragraph -3 of (do shell script “system_profiler SPHardwareDataType”)’ /usr/bin/grep /system/serial-name.txt | /usr/bin/awk -F " " '{print $2}'
| usr/sbin/scutil --set ComputerName
This script finds the serial number, but it doesnt read the whole text file. It seems to just read the first line… When it does read the first line it simply gives me the first serial number on the list and then say command not found.
/system/serial-name.txt: line 1: C07DM37MDD6H: command not found
It also just renames the computer to the serial number since it can not find anyhting.
I am missing something I just cant seem to figure it out.
Any help would be greatly appreciated!
By the way all the macs are either 10.6.8 or 10.7.5 and I am using ARD, hence the UNIX command.
Hi. Welcome to MacScripter.
I don’t know any awk, so I’m not sure what format is implied for your text file. If each line contains the serial number followed by punctuation and/or white space and then the PIN, beginning with an alphanumeric character, this should return the PIN:
This hasn’t been tested with the scutil command on the end as I don’t want to rename my computer! 
Hope this gets you somewhere.
ok problem…
So my text file has every serial number and every pin listed in two columns seperated by a tab.
When it worked I only had one serial and one pin. When I put in the whole text file, it just reads a piece of the list and does not do anything. Any thoughts?
This is the feedback I am getting:
W86487BSVUX 68282
W86487DDVUX 68287
W86488WJVUX 68344
4H649JARWGL 68173
4H649LR4WGL 68175
4H649LT3WGL 68174
W87251D3X1W 71689
W872500DX1W 72253
W8725017X1W 72271
W872500YX1W 72266
W872500ZX1W 72261
W8725010X1W 72272
W8725011X1W 72247
W872500VX1W 72269
W87
What sort of line endings does your text file have? Linefeeds, or returns, or something else? My code assumes linefeeds and doesn’t work with anything else.
Are there any formatting codes in the text apart from tabs and line endings?
Mine are seperated by returns like this:
SerialNumber PIN
W89310Z69GU 88780
W89324K59GU 88782
W89324K99GU 88779
W89324L89GU 88783
W89324PK9GU 88781
W89240PBA9Q 90687
C07CV0Q8DD6H 96083
C07CTTBLDD6H 96084
This works with both returns and linefeeds as a ‘do shell script’ in AppleScript, but the shell code doesn’t return a result in Terminal. I suspect, though, that with the scutil code tagged onto the end, it would do what you want.
do shell script "a=$(system_profiler SPHardwareDataType | sed -En '/Serial Number/ s/[^:]+: //p') ; sed -En '/'$a'/ { s/('$a'[[:blank:]0-9]+).*/\\1/ ; s/.*([0-9]{5}$)/\\1/p ; }' <'/System/serial-name.txt'"
Edit: Here’s an alternative using egrep instead of sed to parse the text for the PIN:
do shell script "a=$(system_profiler SPHardwareDataType | sed -En '/Serial Number/ s/[^:]+: //p') ; egrep -o $a'[[:blank:]0-9]+' <'/System/serial-name.txt' | egrep -ow '[0-9]{5}'"
This is probably good enough for me! With your second script it is outputting the whole line instead of just the pin. Which is way better than what I have been able to do. The first script unfortunately does not work because scutil doesn’t have anything to name it to if that makes sense…
So this is what is happening now:
running this script
a=$(system_profiler SPHardwareDataType | sed -En ‘/Serial Number/ s/[^:]+: //p’) ; egrep -o $a’[[:blank:]0-9]+’ <‘/System/serial-name.txt’ | egrep -ow ‘[0-9]{5}’ | scutil --set ComputerName
Gives me the computer name of:
(serial number) (tab space) (PIN)
Is there a way to get the serial to go away?
I am also very curious why the original script that you gave me worked with only one line but would not work with the whole file… I understand the line feed issue is probably where the problem is, so i will look into that.
Again I am so grateful that you have helped me this far. I really do appreciate it.
The “egrep -ow ‘[0-9]{5}’” in the second script in post #6 is what makes the serial number and the white space “go away”. It explictly returns only five consecutive digits which constitute a complete word in the line returned by the previous egrep command. ie. it returns just the PIN. I’ve now tried it in the terminal with the scutil bit added and the complete code renames my computer to the PIN I invented for it in my test file. Mind you, I’m not doing it with ARD. I don’t know if that makes a difference.
The first script in post #6, as you say, doesn’t give scutil anything with which to work when run in Terminal, which is a mystery to me as it works in the ‘do shell script’ form.
Ok, I came up with a solution.
Since it did work in the applescript editor and not through unix, I just created the applescript and saved it to the system folder also. Now, I can rename them by running this script through ARD
osascript /system/Setname5.scpt
Works perfectly!
Thanks for all of your help. I really do appreciate it very much sir!