Set new computer Name and update in Jamf Pro

kevin_neely
New Contributor III

Small shop so there not much need to automate much but to Rename laptops we've been editing them in Jamf Pro and then running a Policy that connects script:

!/bin/bash

ComputerName="$4"
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName

To an "App" made available via scoping in Self Service. It works but seems like I'm doubling my workload.

I'd like to keep the Self Service app and initiate the naming from it using a script like:

!/bin/bash

newName=$( osascript -e 'text returned of (display dialog "Enter a new name for your Mac..." default answer "" with title "Name Your Computer" with icon file posix file "/System/Library/CoreServices/Finder.app/Contents/Resources/Finder.icns")' )
scutil --set ComputerName "$newName"
echo "Setting ComputerName to $newName"
exit 0

This 2nd script works if I run it from Terminal on the laptop but cannot get it to work on a cloned version of the original Self Service app. I was hoping that if I could get the 2nd script to initiate through Self Service that the "Update Inventory" option under Maintenance could then push that new name to Jamf Pro. I'm fairly certain I'm chasing moondust with this last part, but would still like to get the 2nd script working as a part of an app.

Any help appreciated.

6 REPLIES 6

sharriston
Contributor III

Have you tried instead of using scutil using the Jamf binary to set the computer name. I found it much easier to manage using sudo jamf setcomputername -name $newName. Here is my working script for the issue you are describing.

#!/bin/bash

username=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Please enter the computer name" default answer "" buttons {"Rename"} default button "Rename")
end tell
END)

echo $username
/usr/local/bin/jamf setcomputername -name $username

/usr/local/bin/jamf recon

Hi
This works Fantastic, just 1 q about this. 

It added an icon on the desktop.
And it cant be remove

Got a quick fix for this?

Thanks

I've got the same issue going on with the desktop icon after renaming. Trying to dig into it and find a solution. Anyone found anything on this yet?

kevin_neely
New Contributor III

That did it! Fantastic. Appreciate the help.

Malcolm
Contributor II
            echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
			<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">
  			<xsl:output method=\"text\"/>
  			<xsl:template match=\"/\">
   			<xsl:value-of select=\"id\"/>
  			</xsl:template>
			</xsl:stylesheet>" > /private/var/tmp/stylesheet.xslt

  			curl -s -k \
    		-H "Authorization: Bearer $jamfProApiToken" \
    		-H "Content-type: application/xml" \
    		-X PUT \
    		-d "<computer><purchasing><purchasing_contact>$updatedmdmdeviceName</purchasing_contact></purchasing></computer>" \
    		"${jamfProURL}/JSSResource/computers/id/$computerID"
			echo "name change applied $updatedmdmdeviceName"

  			#curl -k \
    		#-H "Authorization: Bearer $jamfProApiToken" \
    		#-H "Content-type: application/xml" \
    		#-X PUT \
    		#-d "<computer><general><name>$updatedmdmdeviceName</name></general></computer>" \
    		#"${jamfProURL}/JSSResource/computers/id/$computerID"
  			#echo "name change applied $updatedmdmdeviceName"


I have come to share some code, using api to update the MDM record will remove the need to have to do a recon, which in my experience really slows down a setup.

in my example I am currently testing the method by only changing the purchasing > purchasin_contact field for testing purposes, but the commented out section will change the name.

other fields could be used for testing, but this is one we don't use in our environment.

I am working on a greater script which will fix names incorrectly named, and will add prefixes and assigned usernames to the device, where the prefixes are stored within the assigned prestage vendor information, but other fields of the prestage could be used, including the prestage name if it was just the prefix.

the script also, checks for when a generic user is assigned, for shared devices, allowing for a admin user to receive a prompt to rename a device that is incorrectly named.

but as I was always frustrated with not being able to directly change the device name on the MDM, I thought I would share this here. and with the simple example, using the PUT command.

I have a mix of domain joined and Jamf connect devices atm, and some of which that are domain joined, I need to change onsite, and then rebind after a name change, so I have made allowances for that also in my code.
With over 2000 computer devices though, it will be a few weeks, of testing before I allow it to go live.

Malcolm
Contributor II

to get the device ID

#get device serial
strmacbookserial=$(ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}' | tr -d '"')
echo "strmacbookserial $strmacbookserial"

#get device info
# Get device information from Jamf API
  deviceInfo=$( /usr/bin/curl \
  --header "accept: application/xml" \
  --request GET \
  --silent \
  --header "Authorization: Bearer $jamfProApiToken" \
  --url "$jamfProURL/JSSResource/computers/serialnumber/$strmacbookserial" )
  #echo "deviceInfo $deviceInfo"

# Extract computerID info from API response
computerID=$( /usr/bin/xpath -e '/computer/general/id/text()' <<< "$deviceInfo" )
echo "computerID $computerID"

this will collect the ID of the device based on the serial number to use with the command above

this all depends on your ability to code with eh API and establish the Secure Token to run curl commands