Skip to main content
Question

Need help creating script for changing computer name before AD binding


Forum|alt.badge.img+2

Hello! I'm trying to create a script that will allow me to change the computer name to "OX" followed by the serial number of the machine.

I know that I can user the system_profiler | grep "r (system)" to bring up the serial number, but how would I need to append "OX" before the serial number?

Any help would be appreciated! Thanks!

15 replies

mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • November 12, 2013

You would use either a variable for "OX", or simply create a new Computer Name variable with OX + the variable for the Serial #.

So here's a better way to grab the serial number (because system_profiler is a bit slow):

1Serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'["|"]' '/IOPlatformSerialNumber/{print $4}')

Putting "OX" together with the above should be pretty simple (create a "MacName" variable for example)-

1MacName="OX$Serial"

If you wanted something between the two, like a dash, then just add that as well, like this-

1MacName="OX-$Serial"

So now use that in your rename script. Something like (just an example)-

1scutil --set ComputerName "$MacName"

Forum|alt.badge.img+11
  • Valued Contributor
  • 133 replies
  • November 12, 2013

If you use pre-stage imaging in the JSS you can setup OX- to be the prefix to the serial number as the computer name. Very easy to do and requires no scripting. If you need a script to do this and would prefer not to use in the pre-stage feature I can give you an example of what we have done when I have some available time later today.

*EDIT* Looks like mm2270 beat me to it. :)


Forum|alt.badge.img+19
  • Valued Contributor
  • 184 replies
  • November 12, 2013

You might also want to think about setting these too:

1scutil --set LocalHostName
2scutil --set HostName

Forum|alt.badge.img+9
  • Contributor
  • 146 replies
  • November 12, 2013

This is what we use:

1#!/bin/sh
2
3# Rename Computer.sh
4#
5# Renames the computer to the $4, as entered by the script parameter
6# The computer will be unbound from Active Directory, renamed, then rebound.
7# Created by Barnes, Walter on 11/6/12.
8#
9
10# Set computername to the parameter entered by the script
11computername=$4
12
13# if there is no name set exit with an error
14if [ "$computername" == "" ]; then
15 echo "Error: No computername is specified. Please specify a Computer Name"
16 exit 1
17fi
18
19# AD user information for removing the machine from Active Directory
20username=""
21password=""
22
23# Unbind from Active Directory
24echo "Unbinding the computer from Active Directory..."
25/usr/sbin/dsconfigad -r -u "$username" -p "$password"
26
27/usr/bin/killall DirectoryService
28
29# Rename the computer
30echo "Renaming computer to $4"
31
32/usr/sbin/scutil --set ComputerName "$4"
33/usr/sbin/scutil --set LocalHostName "$4"
34/usr/sbin/scutil --set HostName "$4"
35
36# Update Inventory in JSS
37echo "Updating inventory..."
38/usr/sbin/jamf recon
39
40# Rebind to AD by triggering the AD Bind Policy
41#echo "Rebinding the computer to Active Directory..."
42jamf policy -trigger bind
43
44exit 0

Forum|alt.badge.img+11
  • Valued Contributor
  • 215 replies
  • November 12, 2013

Is there a way to grab the last 6 characters of the serial number and enter it into the naming script?

Wanting to use a script with only the last 6 characters as part of the computer name.


Forum|alt.badge.img+1
  • New Contributor
  • 3 replies
  • November 12, 2013

Here you go:

Serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'["|"]' '/IOPlatformSerialNumber/{print $4}') echo ${Serial:(-6)}

or even make it a variable to use in your naming:

last6=echo ${Serial:(-6)}

Forum|alt.badge.img+8
  • Contributor
  • 131 replies
  • November 13, 2013

Be aware... scutil can behave strangely and slowly from time to time for reasons unknown (to us). You may not always get what you expect WHEN you expect it.


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • November 13, 2013

@dmw3][/url][/url][/url][/url - You could also do this to get the last 6 characters-

1Serial=$(ioreg -rd1 -c IOPlatformExpertDevice | awk -F'["|"]' '/IOPlatformSerialNumber/{print $4}' | cut -c 6-)

@lisacherie][/url][/url][/url][/url - good point! You definitely would want to set the other 2 names to the new one as well.

@yellow][/url][/url][/url][/url - if you get dodgy results with scutil, there are other options. For example, with systemsetup:

1/usr/sbin/systemsetup -setcomputername "$MacName"

or with the jamf binary:

1jamf setComputerName -name" $MacName"

In fact, you could actually accomplish the entire renaming directly with the jamf binary.

1sudo jamf setComputerName -useSerialNumber -prefix "OX"

But that would give you the full Serial Number, not just the last 6 characters, or any other variation.


Forum|alt.badge.img+2
  • Author
  • New Contributor
  • 1 reply
  • November 13, 2013

Thanks for the responses guys! This definitely helped me out and gave me some ideas!


Forum|alt.badge.img+5
  • Contributor
  • 72 replies
  • August 16, 2016

Note:

1/usr/sbin/networksetup

&

1/usr/sbin/systemsetup

will error if your computer name is > 31 characters.

For example...

1/usr/sbin/networksetup -setcomputername "01234567890123456789012345678901"
201234567890123456789012345678901 is NOT a valid computer name.
3** Error: The parameters were not valid.
1/usr/sbin/systemsetup -setcomputername "01234567890123456789012345678901"
22016-08-16 16:42:31.348 systemsetup[3368:55826] ### authenticateUsingAuthorizationSync error:Error Domain=com.apple.systemadministration.authorization Code=-60007 "(null)"
3setcomputername: 01234567890123456789012345678901

See blog post for example...
OS X Bonjour Naming Guidelines


Forum|alt.badge.img+5
  • Contributor
  • 20 replies
  • October 26, 2017

I do like this script... Is there a way to have it set so that there's a dialog box to 1) Enter the computer name (through policy) and 2) Request credentials to do the unbinding and rebinding?

Unfortunately, I havn't done much scripting...


Forum|alt.badge.img+5
  • Contributor
  • 20 replies
  • October 30, 2017

Anybody?


easyedc
Forum|alt.badge.img+16
  • Esteemed Contributor
  • 629 replies
  • October 30, 2017

@boanes Others may have different answers, but I've always found that interacting with a. dialogue box was a limitation of JAMF. It had to get called by something else. Many would suggest cocoa dialog, but that's not been updated in a. long time, and for our environment, any tool/app we'd install as a helper would have to go through a certification process. I never found it worth it. When I've needed it, I've leveraged AppleScript to do most of that heavy lifting, and then just export that out as an app. I did have an AppleScript that I'd written LONG ago (like 6 years) but through a few computer changes, when I was first messing with Apple Script. It's VERY rudimentary, and is totally different than what I'd do today, but feel free to leverage it.

In this case, it was for a bunch of minis, which we designated with the prefix MKMM and the last string of their serial.

1#Get user ID
2set getusername to the text returned of (display dialog "Enter your Admin user id here..." default answer "")
3set userid to getusername
4
5set userpass to the text returned of (display dialog "Enter your password.." default answer "" with hidden answer)
6
7set adminpass to the text returned of (display dialog "Enter your local admin password.." default answer "" with hidden answer)
8
9set theserial to (do shell script "system_profiler |grep 'r (system)' | awk '{print $4}'")
10
11
12set count1 to count theserial
13
14if count1 = 11 then set cutserial to (do shell script "system_profiler |grep 'r (system)' | awk '{print $4}' |cut -c 4-11")
15
16if count1 = 12 then set cutserial to (do shell script "system_profiler |grep 'r (system)' | awk '{print $4}' |cut -c 5-12")
17
18if count1 = 13 then set cutserial to (do shell script "system_profiler |grep 'r (system)' | awk '{print $4}' |cut -c 6-13")
19
20if count1 = 14 then set cutserial to (do shell script "system_profiler |grep 'r (system)' | awk '{print $4}' |cut -c 7-14")
21
22set computername to "MKMM" & cutserial
23
24#display dialog computername
25
26#display dialog cutserial
27
28#do shell script "scutil --set ComputerName " & computername with administrator privileges
29#do shell script "scutil --set LocalHostName " & computername with administrator privileges
30
31do shell script "diskutil rename disk0s2 " & computername
32tell application "Terminal"
33 tell window 1
34 activate
35 tell application "System Events"
36 set terminal to application process "Terminal"
37 set frontmost of terminal to true
38
39
40 tell application "System Events"
41 delay "3"
42 keystroke "sudo -i"
43 key code 36
44 delay "1"
45 keystroke adminpass
46 key code 36
47 delay "1"
48 keystroke "scutil --set ComputerName " & computername
49 key code 36
50 delay "2"
51 keystroke "scutil --set LocalHostName " & computername
52 key code 36
53 delay "2"
54 keystroke userpass
55 key code 36
56 delay "75"
57 tell application "Terminal"
58 quit
59
60 end tell
61 end tell
62 end tell
63 end tell
64end tell

wow... looking over this I'm almost too embarrassed to share... almost. good luck. if I have some time upcoming, maybe I'll clean this up.


mm2270
Forum|alt.badge.img+24
  • Legendary Contributor
  • 7886 replies
  • October 30, 2017

@boanes You're looking for a way to input a name manually and then name the computer to that?
If so, Applescript calls can be used there, but the trouble you run into often these days is the OS doesn't like popping up interactive user level dialogs from scripts run under a root context.
But so you know, something like the following would be used to ask for user input, would loop if no text is entered (so it forces something to be entered to dismiss the dialog)

This is a basic example with not much in the way of error checking, checking character length, etc, all of which you'd probably want to do to ensure the name entered won't cause problems when used for the computer name.

1#!/bin/bash
2
3function getResponse ()
4{
5
6response=$(/usr/bin/osascript << EOD
7tell application "System Events"
8set response to text returned of (display dialog "Enter a computer name" default answer "" buttons {"Enter"} default button 1)
9end tell
10EOD)
11
12if [[ "$response" == "" ]]; then
13 getResponse
14fi
15
16}
17
18getResponse
19
20## Put computer naming commands here

Again though, YMMV regarding whether this will actually pop up a dialog when it's run from a Jamf Pro policy, or if it will register a failure due to not being allowed user interaction. I have posted elsewhere on here some examples of how to get Applescripts like this to run as the user (to get input) and then use the response in the remainder of the script. So there are ways to do it.


Forum|alt.badge.img+12
  • Contributor
  • 288 replies
  • October 30, 2017

We used this script to rename, unbind, then rebind to AD. (AD will not automatically update the records when you change the name)

1#!/bin/sh
2
3# HARDCODED VALUES ARE SET HERE
4Pass=""
5
6# CHECK TO SEE IF VALUES WERE PASSED FOR $4, AND IF SO, ASSIGN THEM
7if [ "$4" != "" ] && [ "$Pass" == "" ]; then
8Pass=$4
9fi
10
11# Check to make sure Pass variable was passed down from Casper
12if [ "$Pass" == "" ]; then
13echo "Error: The parameter 'Pass' is blank. Please specify a value."
14exit 1
15fi
16
17# Set Variables
18CompName=$(dsconfigad -show | awk '/Computer Account/{print $NF}' | sed 's/$$//')
19
20# Get current OU of Computer Object
21OU="OU=General Business Computers,OU=####,DC=#####,DC=#####,DC=####"
22
23echo "$OU"
24
25# get machine serial number
26MAC_SERIAL_NUMBER=`ioreg -l | grep IOPlatformSerialNumber|awk '{print $4}' | cut -d " -f 2`
27echo MAC_SERIAL_NUMBER $MAC_SERIAL_NUMBER
28
29serial=$MAC_SERIAL_NUMBER
30
31# set name to machine serial number
32scutil --set ComputerName $serial
33scutil --set HostName $serial
34scutil --set LocalHostName $serial
35
36sleep 2
37
38 ## Begin rebinding process
39
40 #Basic variables
41 computerid=`scutil --get LocalHostName`
42 domain=c##
43 udn=#
44
45 #Advanced variables
46 alldomains="disable"
47 localhome="enable"
48 protocol="smb"
49 mobile="enable"
50 mobileconfirm="disable"
51 user_shell="/bin/bash"
52 admingroups="CorpHelpdesk Operator"
53 namespace="domain"
54 packetsign="allow"
55 packetencrypt="allow"
56 useuncpath="disable"
57 passinterval="30"
58
59 # Bind to AD
60 dsconfigad -add $domain -alldomains $alldomains -username $udn -password $Pass -computer $computerid -ou "$OU" -force -packetencrypt $packetencrypt
61 sleep 1
62 echo "Rebinding to AD and setting advanced options"
63
64 #set advanced options
65 dsconfigad -localhome $localhome
66 sleep 1
67 dsconfigad -groups "$admingroups"
68 sleep 1
69 dsconfigad -mobile $mobile
70 sleep 1
71 dsconfigad -mobileconfirm $mobileconfirm
72 sleep 1
73 dsconfigad -alldomains $alldomains
74 sleep 1
75 dsconfigad -useuncpath "$useuncpath"
76 sleep 1
77 dsconfigad -protocol $protocol
78 sleep 1
79 dsconfigad -shell $user_shell
80 sleep 1
81 dsconfigad -passinterval $passinterval
82 sleep 1
83
84 #dsconfigad adds "All Domains"
85 # Set the search paths to "custom"
86 dscl /Search -create / SearchPolicy CSPSearchPath
87 dscl /Search/Contacts -create / SearchPolicy CSPSearchPath
88
89 sleep 1
90
91 # Add the "corpXXXXXX" search paths
92 dscl /Search -append / CSPSearchPath "/Active Directory/CORP/#####"
93 dscl /Search/Contacts -append / CSPSearchPath "/Active Directory/CORP/#####"
94
95
96 sleep 1
97
98 # Restart opendirectoryd
99 killall opendirectoryd
100 sleep 5
101
102
103exit 0

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