Skip to main content
Question

Problem with naming computer


Forum|alt.badge.img+7

Hey all,
A number of years ago I inherited a Jamf platform that I am trying to improve and clean up. We have a policy of naming computers of your choice. Currently, it consists of three scripts, one of which is an AppleScript to bring up a dialog box where you can enter the desired name of the computer.

This solution has worked fairly well until about a year ago. Now you have to run the script twice for it to work, at least for Ventura and Sonoma.

The script looks like below and I was wondering if someone could help me, give me some tips to make it work better so that you don't have to run it twice?

Thanks in advance.

First script (how it's listed in Jamf policy)

#/bin/sh #Skript som sätter en raett hostname osv. - Made by Marten B. BK.IT #define variables name=$(sudo /usr/sbin/scutil --get ComputerName) sname=$(echo "$name" | tr -cd '[[:alnum:].-]') # set computernames sudo /usr/sbin/scutil --set HostName "$name" sudo /usr/sbin/scutil --set LocalHostName "$sname" sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName -string "$sname" sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder exit 0;


Second script

#!/bin/bash sudo jamf recon sudo jamf policy


Third script (AppleScript)

display dialog ”Set desired computer name: " default answer "like: SE304041-teacher" with icon note buttons {"Cancel", "Continue"} default button "Continue" set ComputerName to text returned of result display dialog ComputerName & " will be set as computer name.." with icon stop buttons {"OK"} default button "OK" do shell script "/usr/sbin/scutil --set ComputerName " & ComputerName & space & "setlocalsubnetname " & ComputerName with administrator privileges

 

4 replies

AJPinto
Forum|alt.badge.img+26
  • Legendary Contributor
  • 2717 replies
  • May 20, 2024

Have you thought about squishing this in to one script? Tossed something together for an idea, but I have not tested this and will need a bit of work to limit what the user can pick but the workflow should be good.

 

 

 

#!/usr/bin/bash #*============================================================================= #* Script Name: #* Created: #* Author: #*============================================================================= #* Purpose: Changes Mac hostname to match user choice. #*============================================================================= #*============================================================================= #* GLOBAL VARIABLES #*============================================================================= computerName=$(scutil --get ComputerName) hostName=$(scutil --get HostName) localHost=$(scutil --get LocalHostName) serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"') #*============================================================================= #* FUNCTIONS #*============================================================================= #*============================================================================= #* Define Hostname #*============================================================================= ###################### # Popup asking for hostname ###################### #this section uses Apple Script to prompt the user to enter a hostname echo "Prompting for Hostname." ## Prompt for Hostname UserChoice=$(/usr/bin/osascript<<END tell application "System Events" activate set the answer to text returned of (display dialog "Enter a hostname" default answer "" buttons {"Continue"} default button 1) end tell END ) #*============================================================================= #* SCRIPT BODY #*============================================================================= userInfo ## Check & Update Computer Name if [ "$computerName" == "$UserChoice" ] then echo "Computer name matches serial number, $UserChoice" else echo "Current Computer Name: $computerName" echo "Computer Name does not meet standards" echo "Changing Computer Name to match Serial Number" scutil --set ComputerName "$UserChoice" fi; $DIV2 ## Check & Update Host Name if [ "$hostName" == "$UserChoice" ] then echo "Host Name matches serial number, $UserChoice" else echo "Current Host Name: $hostName" echo "Host Name does not meet standards" echo "Changing Host Name to match Serial Number" scutil --set HostName "$UserChoice" fi; $DIV2 ## Check & Update Local Host if [ "$localHost" == "$UserChoice" ] then echo "Local Host matches serial number, $UserChoice" else echo "Current Local Host: $localHost" echo "Local Host does not meet standards" echo "Changing Local Host to match Serial Number" scutil --set LocalHostName "$UserChoicer" fi; $DIV2 ## Final Check computerNameII=$(scutil --get ComputerName) hostNameII=$(scutil --get HostName) localHostII=$(scutil --get LocalHostName) echo "Results:"; $DIV3 echo "Serial number: $serialNumber" echo "Computer Name: $computerNameII" echo "Host Name: $hostNameII" echo "Local Host: $localHostII" echo "User entered hostname $UserChoice" if [[ "$computerNameII" == "$UserChoice" ]] && [[ "$hostNameII" == "$UserChoice" ]] && [[ "$localHostII" == "$UserChoice" ]] then echo "Computer Name satisfies naming standards" $DIV1; exit 0 else echo "Computer does not meet naming standards" echo "More troubleshooting will be necessary." $DIV1; exit 1 fi sudo jamf recon #*============================================================================= #* END OF SCRIPT #*=============================================================================

 

 

 


Forum|alt.badge.img+7
  • Author
  • Contributor
  • 91 replies
  • May 21, 2024
AJPinto wrote:

Have you thought about squishing this in to one script? Tossed something together for an idea, but I have not tested this and will need a bit of work to limit what the user can pick but the workflow should be good.

 

 

 

#!/usr/bin/bash #*============================================================================= #* Script Name: #* Created: #* Author: #*============================================================================= #* Purpose: Changes Mac hostname to match user choice. #*============================================================================= #*============================================================================= #* GLOBAL VARIABLES #*============================================================================= computerName=$(scutil --get ComputerName) hostName=$(scutil --get HostName) localHost=$(scutil --get LocalHostName) serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"') #*============================================================================= #* FUNCTIONS #*============================================================================= #*============================================================================= #* Define Hostname #*============================================================================= ###################### # Popup asking for hostname ###################### #this section uses Apple Script to prompt the user to enter a hostname echo "Prompting for Hostname." ## Prompt for Hostname UserChoice=$(/usr/bin/osascript<<END tell application "System Events" activate set the answer to text returned of (display dialog "Enter a hostname" default answer "" buttons {"Continue"} default button 1) end tell END ) #*============================================================================= #* SCRIPT BODY #*============================================================================= userInfo ## Check & Update Computer Name if [ "$computerName" == "$UserChoice" ] then echo "Computer name matches serial number, $UserChoice" else echo "Current Computer Name: $computerName" echo "Computer Name does not meet standards" echo "Changing Computer Name to match Serial Number" scutil --set ComputerName "$UserChoice" fi; $DIV2 ## Check & Update Host Name if [ "$hostName" == "$UserChoice" ] then echo "Host Name matches serial number, $UserChoice" else echo "Current Host Name: $hostName" echo "Host Name does not meet standards" echo "Changing Host Name to match Serial Number" scutil --set HostName "$UserChoice" fi; $DIV2 ## Check & Update Local Host if [ "$localHost" == "$UserChoice" ] then echo "Local Host matches serial number, $UserChoice" else echo "Current Local Host: $localHost" echo "Local Host does not meet standards" echo "Changing Local Host to match Serial Number" scutil --set LocalHostName "$UserChoicer" fi; $DIV2 ## Final Check computerNameII=$(scutil --get ComputerName) hostNameII=$(scutil --get HostName) localHostII=$(scutil --get LocalHostName) echo "Results:"; $DIV3 echo "Serial number: $serialNumber" echo "Computer Name: $computerNameII" echo "Host Name: $hostNameII" echo "Local Host: $localHostII" echo "User entered hostname $UserChoice" if [[ "$computerNameII" == "$UserChoice" ]] && [[ "$hostNameII" == "$UserChoice" ]] && [[ "$localHostII" == "$UserChoice" ]] then echo "Computer Name satisfies naming standards" $DIV1; exit 0 else echo "Computer does not meet naming standards" echo "More troubleshooting will be necessary." $DIV1; exit 1 fi sudo jamf recon #*============================================================================= #* END OF SCRIPT #*=============================================================================

 

 

 


Thanks. I'll give it a try :)


dlondon
Forum|alt.badge.img+14
  • Honored Contributor
  • 375 replies
  • May 22, 2024

@jonrosYou can do this all more simply using the Jamf binary.  That has the ability to change the computer name and does everything you have done making your script smaller.  The manual command if being done by yourself would be

 

 

sudo jamf setcomputername -name whatevernameyouwant

 

 

More info if you check the help for jamf

 

 

jamf help jamf help setcomputername

 

 


Forum|alt.badge.img+7
  • Author
  • Contributor
  • 91 replies
  • May 22, 2024
AJPinto wrote:

Have you thought about squishing this in to one script? Tossed something together for an idea, but I have not tested this and will need a bit of work to limit what the user can pick but the workflow should be good.

 

 

 

#!/usr/bin/bash #*============================================================================= #* Script Name: #* Created: #* Author: #*============================================================================= #* Purpose: Changes Mac hostname to match user choice. #*============================================================================= #*============================================================================= #* GLOBAL VARIABLES #*============================================================================= computerName=$(scutil --get ComputerName) hostName=$(scutil --get HostName) localHost=$(scutil --get LocalHostName) serialNumber=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}') serialNumberII=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}' | tr -d '"') #*============================================================================= #* FUNCTIONS #*============================================================================= #*============================================================================= #* Define Hostname #*============================================================================= ###################### # Popup asking for hostname ###################### #this section uses Apple Script to prompt the user to enter a hostname echo "Prompting for Hostname." ## Prompt for Hostname UserChoice=$(/usr/bin/osascript<<END tell application "System Events" activate set the answer to text returned of (display dialog "Enter a hostname" default answer "" buttons {"Continue"} default button 1) end tell END ) #*============================================================================= #* SCRIPT BODY #*============================================================================= userInfo ## Check & Update Computer Name if [ "$computerName" == "$UserChoice" ] then echo "Computer name matches serial number, $UserChoice" else echo "Current Computer Name: $computerName" echo "Computer Name does not meet standards" echo "Changing Computer Name to match Serial Number" scutil --set ComputerName "$UserChoice" fi; $DIV2 ## Check & Update Host Name if [ "$hostName" == "$UserChoice" ] then echo "Host Name matches serial number, $UserChoice" else echo "Current Host Name: $hostName" echo "Host Name does not meet standards" echo "Changing Host Name to match Serial Number" scutil --set HostName "$UserChoice" fi; $DIV2 ## Check & Update Local Host if [ "$localHost" == "$UserChoice" ] then echo "Local Host matches serial number, $UserChoice" else echo "Current Local Host: $localHost" echo "Local Host does not meet standards" echo "Changing Local Host to match Serial Number" scutil --set LocalHostName "$UserChoicer" fi; $DIV2 ## Final Check computerNameII=$(scutil --get ComputerName) hostNameII=$(scutil --get HostName) localHostII=$(scutil --get LocalHostName) echo "Results:"; $DIV3 echo "Serial number: $serialNumber" echo "Computer Name: $computerNameII" echo "Host Name: $hostNameII" echo "Local Host: $localHostII" echo "User entered hostname $UserChoice" if [[ "$computerNameII" == "$UserChoice" ]] && [[ "$hostNameII" == "$UserChoice" ]] && [[ "$localHostII" == "$UserChoice" ]] then echo "Computer Name satisfies naming standards" $DIV1; exit 0 else echo "Computer does not meet naming standards" echo "More troubleshooting will be necessary." $DIV1; exit 1 fi sudo jamf recon #*============================================================================= #* END OF SCRIPT #*=============================================================================

 

 

 


Tried the script but get an error shown bellow. 🤔 🤔

 


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