Set computer name script - Need help

rcarey
New Contributor III

Hello all,

I'm trying to write a script that allows an end user to set the name of their computer based on a specific naming convention. Unfortunately, my scripting knowledge is lacking and I'm having trouble getting the results I'm looking for.

Here's what I've got so far. Any help would be greatly appreciated. I'm sure it's a bit of a hodge-podge so please forgive my hodge-podginess.

#!/bin/sh


#The user will choose their department from a drop down menu

choose from list {"ACAD", "ALUM", "ANES", "BIOC"} with title "Departments" with prompt "Please choose your department"

#The user will choose their building from a drop down menu

choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI"} with title "Buildings" with prompt "Please choose your building"

#The user will input the value of their asset tag

display dialog "What is your MCW asset tag number " default answer "" buttons {"Continue"} default button 1
text returned of the result

#computerName=$($dept-$bldg-$tag)
#echo $computerName

#set all the name in all the places
#scutil --set ComputerName "$computerName"
#scutil --set LocalHostName "$computerName"
#scutil --set HostName "$computerName"
1 ACCEPTED SOLUTION

howardgmac
New Contributor III

@rcarey Try this:

#!/bin/sh

#  Name Your Computer.sh
#  
#
#  Created by Carey-Peterson, Rob on 10/25/19.
#  


#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Department to (choose from list {"ACAD", "ALUM", "ANES", "BIOC", "IS"} with title "Departments" with prompt "Please choose your department")
end tell
EOD)

#The user will choose their building from a drop down menu

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Building to (choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building")
end tell
EOD)

#The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="$dept-$bldg-$tag"

#set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon

View solution in original post

22 REPLIES 22

mm2270
Legendary Contributor III

The main issue with what you have there is that you are calling some AppleScript related functions in bash, but aren't telling bash to use AppleScript for those (more specifically osascript).

For all the sections where you are trying to use things like choose from list, those are not from bash, they are AS related. So here's what you need to do. Change items like:

choose from list {"ACAD", "ALUM", "ANES", "BIOC"} with title "Departments" with prompt "Please choose your department"

to something like this:

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
choose from list {"ACAD", "ALUM", "ANES", "BIOC"} with title "Departments" with prompt "Please choose your department"
end tell
EOD)

First off, you need to capture the result of the selection, which is what it's doing by creating a variable to run the command in (dept=) It also uses a HEREDOC format to place the AppleScript commands into, which makes it a little easier to use them without worrying about any formatting changes.
Make similar changes to the other sections, picking unique variable names to capture the user selection in, like bldg maybe for the building and tag for the asset tag, using your examples.

Then you should be able to use those variables to create the final computer name just as in your commented out line:

computerName=$($dept-$bldg-$tag)

In addition to these changes, one thing I would consider doing is placing some of those calls into discrete functions, especially the asset tag one, since that involves a user needing to type something in. Putting them into a function allows you to check the result of the input, and if it's blank, just call the function again so it forces them to enter something. Right now, someone could hit Return on that Asset tag screen and enter a blank value, which the script will happily take and then possibly cause a computer naming issue.

Hope those tips help. Post back if you run into any problems.

rcarey
New Contributor III

@mm2270 Thanks for the help with this! I think I have a solid work flow set up for the script now, and in my testing it runs and doesn't give me any errors - but it's not setting the name in the Jamf Pro console. Here's what I got - let me know if you see anything that would be preventing it from completing

#!/bin/sh

#  Name Your Computer.sh
#  
#
#  Created by Carey-Peterson, Rob on 10/25/19.
#  


#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
choose from list {"ACAD", "ALUM", "ANES", "BIOC", "IS"} with title "Departments" with prompt "Please choose your department"
end tell
EOD)

#The user will choose their building from a drop down menu

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building"
end tell
EOD)

#The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1
end tell
EOD)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

computerName=$($dept-$bldg-$tag)
echo $computerName

#Get Computer ID in JSS 
id=$(curl -sku username:password -H "content-type: text/xml" https://jamfconsole.website.com:8443/JSSResource/computers/id | xmllint --format -)

curl -sku username:password -H "content-type: text/xml" https://jamfconsole.website.com:8443/JSSResource/computers/id/$id -X PUT -d "<computer><general><name>$computerName</name></general></computer>"

mm2270
Legendary Contributor III

@rcarey The problem I see is that you have to instruct AppleScript to capture the input in your commands. Here are examples.
For the choose from list window styles, use this instead:

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Building to (choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building")
end tell
EOD)

Notice I added the set Building to and enclosed the entire command in parenthesis.

For the text field window, it's slightly different. Use this:

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)

Adding those to each command as appropriate should capture the input or selections made into the variables, and then it should work to rename the computer. The reason it probably wasn't working before was because the variables were empty.

rcarey
New Contributor III

@mm2270

Made that change, but no avail. Still not setting it in the Jamf Console even though the policy runs all the way through.

Looking at the log the strangest part that gets returned is:

Script result: /Library/Application Support/JAMF/tmp/Rob Naming Test: line 40: IS-CAIR-8402: command not found
-:10: parser error : Opening and ending tag mismatch: br line 8 and p
</p>
    ^
-:11: parser error : Opening and ending tag mismatch: p line 8 and body
</body>
       ^
-:12: parser error : Opening and ending tag mismatch: body line 5 and html
</html>
       ^
-:13: parser error : Premature end of data in tag html line 1

^
<html>
<head>
   <title>Status page</title>
</head>
<body style="font-family: sans-serif;">
<p style="font-size: 1.2em;font-weight: bold;margin: 1em 0px;">Not Found</p>
<p>The server has not found anything matching the request URI</p>
<p>You can get technical details <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.5">here</a>.<br>
Please continue your visit at our <a href="/">home page</a>.
</p>
</body>
</html>

If no other thoughts on this, all good. You've been more than helpful. Might reach out to Jamf support if necessary to help determine what I might be missing.

mm2270
Legendary Contributor III

Hey @rcarey I didn’t even see it before but it looks like you’re doing an API call to update the computer name? You shouldn’t need to do that. Just a simple recon should update the computer name. Instead of the curl command at the end, try just doing:

/usr/local/bin/jamf recon

See if that fixes it.

howardgmac
New Contributor III

@rcarey May I suggest an alternative set of commands for actually setting the name:

/usr/local/bin/jamf setcomputername -name "$computerName"
/usr/local/bin/jamf recon

The first line will perform the steps the 3 scutil lines did in your first posting of the script.
The second will update the name in JAMF and ensure you have an updated computer record for that computer.

rcarey
New Contributor III

@mm2270 , That is a much more elegant solution than trying to run a curl command.

Last hiccup I seem to be running into, and I'm testing against running it locally is that at the end of my script, after collecting the data input by the user, I'm running this: EDIT* - Included entire script as I currently have it for reference

#!/bin/sh

#  Name Your Computer.sh
#  
#
#  Created by Carey-Peterson, Rob on 10/25/19.
#  


#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Department to (choose from list {"ACAD", "ALUM", "ANES", "BIOC", "IS"} with title "Departments" with prompt "Please choose your department")
end tell
EOD)

#The user will choose their building from a drop down menu

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Building to (choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building")
end tell
EOD)

#The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName=$($dept-$bldg-$tag)

#set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon

The script in Self Service seems to stall after I input the data. However when I tested with the script locally in Terminal, it's actually looking for input again rather than taking the $deviceName variable and applying it. It's definitely setting the variable because I can see it both in the logs and in the local terminal when testing the script.

@howardgmac - Added your recommendation. Still no luck with setting the local Computer Name unfortunately.

Any thoughts?

howardgmac
New Contributor III

@rcarey Try this:

#!/bin/sh

#  Name Your Computer.sh
#  
#
#  Created by Carey-Peterson, Rob on 10/25/19.
#  


#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Department to (choose from list {"ACAD", "ALUM", "ANES", "BIOC", "IS"} with title "Departments" with prompt "Please choose your department")
end tell
EOD)

#The user will choose their building from a drop down menu

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Building to (choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building")
end tell
EOD)

#The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)


#This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="$dept-$bldg-$tag"

#set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon

rcarey
New Contributor III

@howardgmac

Welp...that worked great. Was it seriously the case of a stupid extra $? Or did I miss something else you adjusted?

HUGE thank you to you as well as @mm2270 . I'm super grateful for the help with this.

csanche3x
New Contributor II

Hey howardgmac,

Thanks for the script this is exactly what I was looking for, I'm testing the script "As-Is" and I do get the input prompts but the computer name comes up as (- -) the two sepatator dashes without the input values I selected. What am I missing?

(btw I'm using Catalina)

Thanks,

Carlos

mm2270
Legendary Contributor III

@csanche3x Not that this is much help, but I pulled down the exact script above that @howardgmac posted and tested it locally, but commented out the lines that actually renamed the Mac and put in an echo instead to echo back the result, and it worked ok for me on Catalina. It correctly captured my inputs and set the name variable to what I expected.
If that isn't working for you, I'd suggest commenting out the rename lines and instead having the script echo back what it thinks should be the new computer name to see if its right or not. if it's showing up like you see in the final result with - - or such, then something is wrong in capturing the results. If it shows the right name, but still renames them incorrectly, then it's the lines that rename the Mac that are likely the problem.

Hope that helps.

csanche3x
New Contributor II

@mm2270 Thank you for your assistance! can you please share your working script?, I'm still having issues getting it to work as expected.

Thank you in Advance.!

mm2270
Legendary Contributor III

@csanche3x As I mentioned, my "working script" was really just the script posted above by @howardgmac, but I commented out the lines that do the renaming and the recon.
However, in looking back at the script posted, I think I see the issue. The jamf verb to set the computer name is actually case sensitive. it should be /usr/local/bin/jamf setComputerName -name "$deviceName" Note the upper case C and N in the verb. I don't think it will work with setcomputername as it was written. So that might be the issue. I would try making that edit and see if it works.

csanche3x
New Contributor II

@mm2270 I corrected the syntax as you suggested but I'm still getting the same error messsages and the same results ( - - ). I 'm using Catalina and I did chage my default shell to sh.
e3f42421f6f4436da810e8029ca21cf8

mm2270
Legendary Contributor III

@csanche3x Can you post the exact script you're using? Because those errors are indicating a formatting error somewhere in the script. It looks like maybe one of the variables isn't being closed correctly, perhaps missing a final ) in it. But if you're able to post it here so we can look at it that would help.

csanche3x
New Contributor II

@ mm2270 here it is:

!/bin/sh

Name Your Computer.sh

Created by Carey-Peterson, Rob on 10/25/19.

#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Department to (choose from list {"ACAD", "ALUM", "ANES", "BIOC", "IS"} with title "Departments" with prompt "Please choose your department")
end tell
EOD)

The user will choose their building from a drop down menu

bldg=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set Building to (choose from list {"APM", "BRI", "CAIR", "CFAC", "CCC", "CHCB", "CHW", "CRI", "MEB"} with title "Buildings" with prompt "Please choose your building")
end tell
EOD)

The user will input the value of their asset tag

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD)

This takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API

deviceName="$dept-$bldg-$tag"

set all the name in all the places

/usr/local/bin/jamf setComputerName -name "$deviceName"
/usr/local/bin/jamf recon

mm2270
Legendary Contributor III

OK, I'm not seeing anything wrong with that script at first glance. It looks ok from here, but I'll take a closer look to see if there's something off about it.

For the future though, to make scripts easier to read on this forum, you should surround the entire script in the code markdown tags. You can add the tags ``` before the start of the script and after it. Doing that will format the script to make it easier to read here.

davidelkinbram
New Contributor

Your ending HERDOC lines need to be on their own, move the closing parenthesis to the next line.
Should parse ok after that.

tag=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set AssetTag to text returned of (display dialog "What is your MCW asset tag number?" default answer "" buttons {"Continue"} default button 1)
end tell
EOD
)

naschenbrenner
New Contributor III
New Contributor III

Sorry to revive such an old thread, but if I wanted to have the full department listed for ease of use but then have it shortened in the actual computer name how would I do that? Example being the drop down would show "Creative" "Marketing", etc. and if they selected Creative the output into the computer name would be CRTV. Thanks for getting me this far!

There are many approaches you could try.  Arrays can be fun, but for this I'd suggest using the case/esac shell construct:
https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

case "$tag" in
  'Creative')
    tag_short='CRTV'
    ;;

  'Marketing')
    tag_short='MKTG'
    ;;

  *)
    ## should always be last in the list, if you need a catch-all for unmatched items
    tag_short='DEFAULT'
    ;;
esac



That was exactly what I needed, thank you very much @davidelkinbram!

lsv
New Contributor III

Apologies for revitalizing an old thread again.

 

Is there a way that you can have these prompts wait for user input? I have a script at the moment based on the one provided by @howardgmac, but for some reason if the end user doesn't provide an input the prompts will still cycle through in stead of waiting for user input. My script is below, any guidance would be greatly appreciated!


 

#!/bin/sh

#  Name Your Computer.sh

#get logged in user info
loggedInUser=$3

#Icon location
brandingImage="/Users/$loggedInUser/Library/Application Support/com.jamfsoftware.selfservice.mac/Documents/Images/brandingimage.png"

#The user will be notified that we are going to change the name of their computer
popUp=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set popUp to text returned of (display dialog "We are running a policy that will rename your machine, press continue to begin the process (this will take 1 minute)." buttons {"Continue"} default button 1 with icon posix file "$brandingImage")
end tell
EOD)

#The user will input the value of their first name

firstName=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set firstName to text returned of (display dialog "What is your first name?" default answer "" buttons {"Continue"} default button 1 with icon posix file "$brandingImage")
end tell
EOD)

#The user will input the value of their first name

lastName=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set lastName to text returned of (display dialog "What is your last name?" default answer "" buttons {"Continue"} default button 1 with icon posix file "$brandingImage")
end tell
EOD)

#The user will input the value of their first name

email=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set email to text returned of (display dialog "What is your veda email?" default answer "" buttons {"Continue"} default button 1 with icon posix file "$brandingImage")
end tell
EOD)

#The user will choose their department from a drop down menu

dept=$(/usr/bin/osascript << EOD
tell application "System Events"
activate
set dept to (choose from list {"Corporate", "Delivery", "Marketing", "Sales", "Science", "Success", "Technology"} with title "Departments" with prompt "Please choose your department")
end tell
EOD)

#Groups first and last name
realName="$firstName $lastName"

#Takes the data collected from the user and sets it as the Computer Name and submits the name to the Jamf API
deviceName="$realName - Macbook Pro"

#Set all the name in all the places
/usr/local/bin/jamf setcomputername -name "$deviceName"
/usr/local/bin/jamf recon -endUsername "$firstName$lastName" -realname "$realName" -email "$email" -position " " -building "test bldg" -department "$dept" -phone " " -room " "