Storing Data in JAMF (Not in a Computer or User Record) For Naming Purposes

HNTIT
Contributor II

Anyone know how to do this ? If it's possible.

I am looking for a way to store a list of Serial Numbers and Machine Names so that when my DEP Prestage devices come online they get the correct name without intervention, at the moment we can only get so far then we have to Set the Computer Name on the Machine record.

We do not use the Serial Number or Asset Tag as part of the Naming Standard so I can't script round it or use an Inventory Preload

1 ACCEPTED SOLUTION

Headbolt
New Contributor II

I spent an age looking into this very issue, and I modified my existing to add this function a few weeks back, just tidied the formatting up before posting.

Feel free, it seems to do the job for me.

#!/bin/bash
#
###############################################################################################################################################
#
# ABOUT THIS PROGRAM
#
#   This Script is designed for use in JAMF
#
#   - This script will ...
#       Grab the Machines name Locally
#       Grab the Machines Target name from the JAMF Extension Attribute
#           Grab the Machines Entry in the Preload Inventory Table if it exists - From V2 Onwards
#       Rename if needed
#           Update or create the Machines Entry in the Preload Inventory Table if it needed - From V2 Onwards
#
###############################################################################################################################################
#
# HISTORY
#
#   Version: 1.1 - 11/10/2019
#
#   - 13/03/2018 - V1.0 - Created by Headbolt
#
#   - 11/10/2019 - V1.1 - Updated by Headbolt
#                           Support added for Preload Inventory Table
#                           More comprehensive error checking and notation
#
###############################################################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
###############################################################################################################################################
#
# Variables used by this script.
#
# Grab the password for API Login from JAMF variable #4 eg. username
apiUser=$4
#
# Grab the password for API Login from JAMF variable #5 eg. password
apiPass=$5
#
# Grab the first part of the API URL from JAMF variable #6 eg. https://COMPANY-NAME.jamfcloud.com
apiURL=$6
#
# Set the name of the script for later logging
ScriptName="append prefix here as needed - Check and Rename Machine Based On EA Value"
#
###############################################################################################################################################
# 
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
###############################################################################################################################################
#
# Defining Functions
#
###############################################################################################################################################
#
# Data Gathering Function
#
GatherData(){
#
## This gets the Mac's current name
macName=$(scutil --get ComputerName)
#
## This gets the Mac's Serial Number
serial=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')
#
## This gets the Target Computer Name (Extension Attribute) From The JAMF Object Matching the Serial Number of the Machine
TargetComputerName=$(/usr/bin/curl -s -u ${apiUser}:${apiPass} -H "Accept: application/xml" "${apiURL}/JSSResource/computers/serialnumber/${serial}" | /usr/bin/xpath '/computer/extension_attributes/extension_attribute[name="Target Computer Name"]/value/text()' 2>/dev/null)
#
## This Authenticates against the JAMF API with the Provided details and obtains an Authentication Token
rawtoken=$(curl -s -u ${apiUser}:${apiPass} -X POST "${apiURL}/uapi/auth/tokens" | grep token)
rawtoken=${rawtoken%?};
token=$(echo $rawtoken | awk '{print$3}' | cut -d " -f2)
#
## This Searches the Preload Inventory Table looking for the Serial Number of the machine
#
preloadEntryA=$(curl -s -X GET "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload?page=0&size=100&sort=id%3Aasc" -H 'Authorization: Bearer '$token'' | grep -B 1 ${serial})
preloadEntryB=$(curl -s -X GET "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload?page=0&size=100&sort=id%3Aasc" -H 'Authorization: Bearer '$token'' | grep -A 4 ${serial})
#
## This Searches the Preload Inventory Entry for the Machines Entry ID
preloadEntryID=$(echo $preloadEntryA | awk -F ',' '{print $1 FS ""}' | rev | cut -c 2- | rev | cut -c 8-)
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the presence of any Extension Attributes
preloadEAentry=$(echo $preloadEntryB | grep "extensionAttributes")
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the presence of a "Target Computer Name" Extension Attribute
preloadEAentryTCN=$(echo $preloadEntryB | grep "Target Computer Name")
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the Value of a "Target Computer Name" Extension Attribute
preloadEAentryTCNValue=$(echo $preloadEAentryTCN | awk -F 'value' '{print $2 FS ""}' | cut -c 6-  | awk -F '"' '{print $1 FS ""}' | rev | cut -c 2- | rev)
#
## These Loops check the status of the Preload Entries to ensure all parts are present before attempting to process them
#
if [ "$preloadEntryB" == "" ]
    then
        preloadEntryPresent=Not-Present
    else
        preloadEntryPresent=Present
        #        

        if [ "$preloadEntryID" == "" ]
            then
                preloadEntryIDPresent=Not-Present
            else
                preloadEntryIDPresent=Present
                #
                if [ "$preloadEAentry" == "" ]
                    then
                        preloadEAentryPresent=Not-Present
                    else
                        preloadEAentryPresent=Present
                        #
                        if [ "$preloadEAentryTCN" == "" ]
                            then
                                preloadEAentryTCNPresent=Not-Present
                            else
                                preloadEAentryTCNPresent=Present
                                #
                                if [ "$preloadEAentryTCNValue" == "" ]
                                    then
                                        preloadEAentryTCNValuePresent=Not-Present
                                    else
                                        preloadEAentryTCNValuePresent=Present
                                fi
                        fi
                fi
        fi
fi
#
}
#
###############################################################################################################################################
#
# Check Function
#
Check(){
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
## Outputs Current Computer Name
/bin/echo Current Computer Name = $macName
#
## Outputs Computers Serial Number
/bin/echo Computer Serial Number = $serial
#
## Outputs Target Computer Name
/bin/echo Target Computer Name = $TargetComputerName
#
## Outputs Status of Preload Inventory Entry if present
if [ "$preloadEntryB" != "" ]
    then
        /bin/echo Preload Inventory Entry for Serial Number $serial is $preloadEntryPresent
        #
        if [ "$preloadEntryID" != "" ]
            then
                /bin/echo Preload Inventory Entry ID Serial Number $serial is $preloadEntryID
                #
                if [ "$preloadEAentry" != "" ]
                    then
                        ## Outputs Status of Preload Inventory EA Entry if present
                        /bin/echo Preload Inventory EA Entry for Serial Number $serial is $preloadEAentryPresent
                        ## Outputs Status of Preload Inventory EA "Target Computer Name" Entry if present
                        if [ "$preloadEAentryTCN" != "" ]
                            then
                                /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNPresent
                                ## Outputs Preload Inventory EA "Target Computer Name" Value if present
                                if [ "$preloadEAentryTCNValue" != "" ]
                                    then
                                        /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial = $preloadEAentryTCNValue
                                    else
                                        /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNValuePresent
                                fi
                            else
                                /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNPresent
                        fi
                    else
                        /bin/echo Preload Inventory EA Entry for Serial Number $serial is $preloadEAentryPresent
                fi
        fi
fi
#
}
#
###############################################################################################################################################
#
# Section End Function
#
SectionEnd(){
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
# Outputting a Dotted Line for Reporting Purposes
/bin/echo  -----------------------------------------------
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
}
#
###############################################################################################################################################
#
# Script End Function
#
ScriptEnd(){
#
# Outputting a Blank Line for Reporting Purposes
#/bin/echo
#
/bin/echo Ending Script '"'$ScriptName'"'
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
# Outputting a Dotted Line for Reporting Purposes
/bin/echo  -----------------------------------------------
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
}
#
###############################################################################################################################################
#
# End Of Function Definition
#
###############################################################################################################################################
#
# Beginning Processing
#
###############################################################################################################################################
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
SectionEnd
#
/bin/echo "Grabbing current Values"
GatherData
#
SectionEnd
#
/bin/echo "Checking current Values"
Check
#
SectionEnd
#
## Now compare the Extension Attribute value with the current computer name
if [ ! -z "$TargetComputerName" ]
    then
        if [[ "$macName" != "$TargetComputerName" ]]
            then
                ## Rename the Mac to the assigned name
                /bin/echo Renaming Machine to $TargetComputerName
                /usr/local/bin/jamf setComputerName -name "$TargetComputerName"
                dscacheutil -flushcache
                #
                if [ "$preloadEntryB" != "" ]
                    then
                        if [[ "$preloadEAentryTCNValue" != "$TargetComputerName" ]]
                            then
                                /bin/echo New Machine Name and Preload Inventory EA "Target Computer Name" Entry for Serial Number $serial Do Not Match 
                                /bin/echo Deleting Preload Inventory Record ID $preloadEntryID
                                DeleteOutcome=$(curl -s -X DELETE -H 'Authorization: Bearer '$token'' -H "accept: application/json" -H "Content-Type: application/json" https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload/$preloadEntryID)
                                DeleteOutput=$(/bin/echo $DeleteOutcome | grep error)
                                if [ "$DeleteOutput" != "" ]
                                    then
                                        echo $DeleteOutput
                                fi        
                                #
                                /bin/echo Uploading Preload Inventory Entry
                                UploadOutcome=$(curl -s -X POST "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload" -H 'Authorization: Bearer '$token'' "accept: application/json" -H "Content-Type: application/json" -d "{ "id": 0, "serialNumber": "$serial", "deviceType": "Computer", "extensionAttributes": [ { "name": "Target Computer Name", "value": "$TargetComputerName" } ]}")
                                UploadOutput=$(/bin/echo $UploadOutcome | grep error)
                                if [ "$UploadOutput" != "" ]
                                    then
                                        echo $UploadOutput
                                fi        
                            else
                                /bin/echo New Machine Name and Preload Inventory EA "Target Computer Name" Entry for Serial Number $serial Match
                        fi
                        #
                    else
                        /bin/echo Uploading Preload Inventory Entry
                        UploadOutcome=$(curl -s -X POST "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload" -H 'Authorization: Bearer '$token'' "accept: application/json" -H "Content-Type: application/json" -d "{ "id": 0, "serialNumber": "$serial", "deviceType": "Computer", "extensionAttributes": [ { "name": "Target Computer Name", "value": "$TargetComputerName" } ]}")
                        UploadOutput=$(/bin/echo $UploadOutcome | grep error)
                        if [ "$UploadOutput" != "" ]
                            then
                                echo $UploadOutput
                        fi        
                fi
                #
                SectionEnd
                #
                ## Re-Checking Machine Name
                /bin/echo "Grabbing New Values"
                GatherData
                # 
                SectionEnd
                #
                /bin/echo "Checking New Values"
                Check
                #               
                SectionEnd
                #
                ScriptEnd
                #
                exit 0
                #
            else
                /bin/echo MATCH > /var/JAMF/Name+TargetName-Match.txt
                /bin/echo "Mac name already matches assigned name. Writing Marker File"
                /bin/echo "/var/JAMF/Name+TargetName-Match.txt"
                # 
                ScriptEnd
                #
                exit 0
        fi
    else
        #
        /bin/echo "Could not get assigned name from computer record"
        #
        ScriptEnd
        #
        exit 1
fi
#
ScriptEnd

View solution in original post

6 REPLIES 6

dpertschi
Valued Contributor

Have a look at Inventory Preload, this is pretty cool.
https://docs.jamf.com/10.15.0/jamf-pro/administrator-guide/Inventory_Preload.html

HNTIT
Contributor II

Dosent really help much unless i start messing about with Extension Attributes and doing it that way.

Can you use a script or the API to read/write the Inventory Preload ?

wmehilos
Contributor

I keep a serial number,machine name CSV file on GitHub. An enrollment script curls it down to /tmp, then just searches for the device's serial in the list, and sets the ComputerName with scutil. Keeping it on GitHub and editing it on GitHub gives you the added benefit of being able to track changes over time and undo mistakes fairly quickly.

#!/bin/zsh
IFS=","
mySerial=`ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}'`
TOKEN="$4"

curl -s $5 > /tmp/serialmaster.csv

NAME=`grep "$mySerial" /tmp/serialmaster.csv | awk -F, '{print $2}'`

/usr/sbin/scutil --set ComputerName "$NAME" 

/usr/local/bin/jamf recon

The GitHub repo I use to store the csv is private, so I have to pass a token to $4. To $5 I pass a string like the following:

https://$TOKEN@raw.githubusercontent.com/$YOURGITHUBUSERNAME/$YOURGITHUBREPO/master/SN_Name_Master.csv

If you're comfortable keeping the Serial/Name list in a public repo, you can just modify the curl command to suit your needs.

Headbolt
New Contributor II

I spent an age looking into this very issue, and I modified my existing to add this function a few weeks back, just tidied the formatting up before posting.

Feel free, it seems to do the job for me.

#!/bin/bash
#
###############################################################################################################################################
#
# ABOUT THIS PROGRAM
#
#   This Script is designed for use in JAMF
#
#   - This script will ...
#       Grab the Machines name Locally
#       Grab the Machines Target name from the JAMF Extension Attribute
#           Grab the Machines Entry in the Preload Inventory Table if it exists - From V2 Onwards
#       Rename if needed
#           Update or create the Machines Entry in the Preload Inventory Table if it needed - From V2 Onwards
#
###############################################################################################################################################
#
# HISTORY
#
#   Version: 1.1 - 11/10/2019
#
#   - 13/03/2018 - V1.0 - Created by Headbolt
#
#   - 11/10/2019 - V1.1 - Updated by Headbolt
#                           Support added for Preload Inventory Table
#                           More comprehensive error checking and notation
#
###############################################################################################################################################
#
# DEFINE VARIABLES & READ IN PARAMETERS
#
###############################################################################################################################################
#
# Variables used by this script.
#
# Grab the password for API Login from JAMF variable #4 eg. username
apiUser=$4
#
# Grab the password for API Login from JAMF variable #5 eg. password
apiPass=$5
#
# Grab the first part of the API URL from JAMF variable #6 eg. https://COMPANY-NAME.jamfcloud.com
apiURL=$6
#
# Set the name of the script for later logging
ScriptName="append prefix here as needed - Check and Rename Machine Based On EA Value"
#
###############################################################################################################################################
# 
# SCRIPT CONTENTS - DO NOT MODIFY BELOW THIS LINE
#
###############################################################################################################################################
#
# Defining Functions
#
###############################################################################################################################################
#
# Data Gathering Function
#
GatherData(){
#
## This gets the Mac's current name
macName=$(scutil --get ComputerName)
#
## This gets the Mac's Serial Number
serial=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')
#
## This gets the Target Computer Name (Extension Attribute) From The JAMF Object Matching the Serial Number of the Machine
TargetComputerName=$(/usr/bin/curl -s -u ${apiUser}:${apiPass} -H "Accept: application/xml" "${apiURL}/JSSResource/computers/serialnumber/${serial}" | /usr/bin/xpath '/computer/extension_attributes/extension_attribute[name="Target Computer Name"]/value/text()' 2>/dev/null)
#
## This Authenticates against the JAMF API with the Provided details and obtains an Authentication Token
rawtoken=$(curl -s -u ${apiUser}:${apiPass} -X POST "${apiURL}/uapi/auth/tokens" | grep token)
rawtoken=${rawtoken%?};
token=$(echo $rawtoken | awk '{print$3}' | cut -d " -f2)
#
## This Searches the Preload Inventory Table looking for the Serial Number of the machine
#
preloadEntryA=$(curl -s -X GET "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload?page=0&size=100&sort=id%3Aasc" -H 'Authorization: Bearer '$token'' | grep -B 1 ${serial})
preloadEntryB=$(curl -s -X GET "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload?page=0&size=100&sort=id%3Aasc" -H 'Authorization: Bearer '$token'' | grep -A 4 ${serial})
#
## This Searches the Preload Inventory Entry for the Machines Entry ID
preloadEntryID=$(echo $preloadEntryA | awk -F ',' '{print $1 FS ""}' | rev | cut -c 2- | rev | cut -c 8-)
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the presence of any Extension Attributes
preloadEAentry=$(echo $preloadEntryB | grep "extensionAttributes")
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the presence of a "Target Computer Name" Extension Attribute
preloadEAentryTCN=$(echo $preloadEntryB | grep "Target Computer Name")
#
## This Searches the Preload Inventory Entry for the Machines Serial Number looking for the Value of a "Target Computer Name" Extension Attribute
preloadEAentryTCNValue=$(echo $preloadEAentryTCN | awk -F 'value' '{print $2 FS ""}' | cut -c 6-  | awk -F '"' '{print $1 FS ""}' | rev | cut -c 2- | rev)
#
## These Loops check the status of the Preload Entries to ensure all parts are present before attempting to process them
#
if [ "$preloadEntryB" == "" ]
    then
        preloadEntryPresent=Not-Present
    else
        preloadEntryPresent=Present
        #        

        if [ "$preloadEntryID" == "" ]
            then
                preloadEntryIDPresent=Not-Present
            else
                preloadEntryIDPresent=Present
                #
                if [ "$preloadEAentry" == "" ]
                    then
                        preloadEAentryPresent=Not-Present
                    else
                        preloadEAentryPresent=Present
                        #
                        if [ "$preloadEAentryTCN" == "" ]
                            then
                                preloadEAentryTCNPresent=Not-Present
                            else
                                preloadEAentryTCNPresent=Present
                                #
                                if [ "$preloadEAentryTCNValue" == "" ]
                                    then
                                        preloadEAentryTCNValuePresent=Not-Present
                                    else
                                        preloadEAentryTCNValuePresent=Present
                                fi
                        fi
                fi
        fi
fi
#
}
#
###############################################################################################################################################
#
# Check Function
#
Check(){
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
## Outputs Current Computer Name
/bin/echo Current Computer Name = $macName
#
## Outputs Computers Serial Number
/bin/echo Computer Serial Number = $serial
#
## Outputs Target Computer Name
/bin/echo Target Computer Name = $TargetComputerName
#
## Outputs Status of Preload Inventory Entry if present
if [ "$preloadEntryB" != "" ]
    then
        /bin/echo Preload Inventory Entry for Serial Number $serial is $preloadEntryPresent
        #
        if [ "$preloadEntryID" != "" ]
            then
                /bin/echo Preload Inventory Entry ID Serial Number $serial is $preloadEntryID
                #
                if [ "$preloadEAentry" != "" ]
                    then
                        ## Outputs Status of Preload Inventory EA Entry if present
                        /bin/echo Preload Inventory EA Entry for Serial Number $serial is $preloadEAentryPresent
                        ## Outputs Status of Preload Inventory EA "Target Computer Name" Entry if present
                        if [ "$preloadEAentryTCN" != "" ]
                            then
                                /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNPresent
                                ## Outputs Preload Inventory EA "Target Computer Name" Value if present
                                if [ "$preloadEAentryTCNValue" != "" ]
                                    then
                                        /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial = $preloadEAentryTCNValue
                                    else
                                        /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNValuePresent
                                fi
                            else
                                /bin/echo Preload Inventory EA '"'Target Computer Name'"' Entry for Serial Number $serial is $preloadEAentryTCNPresent
                        fi
                    else
                        /bin/echo Preload Inventory EA Entry for Serial Number $serial is $preloadEAentryPresent
                fi
        fi
fi
#
}
#
###############################################################################################################################################
#
# Section End Function
#
SectionEnd(){
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
# Outputting a Dotted Line for Reporting Purposes
/bin/echo  -----------------------------------------------
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
}
#
###############################################################################################################################################
#
# Script End Function
#
ScriptEnd(){
#
# Outputting a Blank Line for Reporting Purposes
#/bin/echo
#
/bin/echo Ending Script '"'$ScriptName'"'
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
# Outputting a Dotted Line for Reporting Purposes
/bin/echo  -----------------------------------------------
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
}
#
###############################################################################################################################################
#
# End Of Function Definition
#
###############################################################################################################################################
#
# Beginning Processing
#
###############################################################################################################################################
#
# Outputting a Blank Line for Reporting Purposes
/bin/echo
#
SectionEnd
#
/bin/echo "Grabbing current Values"
GatherData
#
SectionEnd
#
/bin/echo "Checking current Values"
Check
#
SectionEnd
#
## Now compare the Extension Attribute value with the current computer name
if [ ! -z "$TargetComputerName" ]
    then
        if [[ "$macName" != "$TargetComputerName" ]]
            then
                ## Rename the Mac to the assigned name
                /bin/echo Renaming Machine to $TargetComputerName
                /usr/local/bin/jamf setComputerName -name "$TargetComputerName"
                dscacheutil -flushcache
                #
                if [ "$preloadEntryB" != "" ]
                    then
                        if [[ "$preloadEAentryTCNValue" != "$TargetComputerName" ]]
                            then
                                /bin/echo New Machine Name and Preload Inventory EA "Target Computer Name" Entry for Serial Number $serial Do Not Match 
                                /bin/echo Deleting Preload Inventory Record ID $preloadEntryID
                                DeleteOutcome=$(curl -s -X DELETE -H 'Authorization: Bearer '$token'' -H "accept: application/json" -H "Content-Type: application/json" https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload/$preloadEntryID)
                                DeleteOutput=$(/bin/echo $DeleteOutcome | grep error)
                                if [ "$DeleteOutput" != "" ]
                                    then
                                        echo $DeleteOutput
                                fi        
                                #
                                /bin/echo Uploading Preload Inventory Entry
                                UploadOutcome=$(curl -s -X POST "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload" -H 'Authorization: Bearer '$token'' "accept: application/json" -H "Content-Type: application/json" -d "{ "id": 0, "serialNumber": "$serial", "deviceType": "Computer", "extensionAttributes": [ { "name": "Target Computer Name", "value": "$TargetComputerName" } ]}")
                                UploadOutput=$(/bin/echo $UploadOutcome | grep error)
                                if [ "$UploadOutput" != "" ]
                                    then
                                        echo $UploadOutput
                                fi        
                            else
                                /bin/echo New Machine Name and Preload Inventory EA "Target Computer Name" Entry for Serial Number $serial Match
                        fi
                        #
                    else
                        /bin/echo Uploading Preload Inventory Entry
                        UploadOutcome=$(curl -s -X POST "https://huntsworth.jamfcloud.com/uapi/v1/inventory-preload" -H 'Authorization: Bearer '$token'' "accept: application/json" -H "Content-Type: application/json" -d "{ "id": 0, "serialNumber": "$serial", "deviceType": "Computer", "extensionAttributes": [ { "name": "Target Computer Name", "value": "$TargetComputerName" } ]}")
                        UploadOutput=$(/bin/echo $UploadOutcome | grep error)
                        if [ "$UploadOutput" != "" ]
                            then
                                echo $UploadOutput
                        fi        
                fi
                #
                SectionEnd
                #
                ## Re-Checking Machine Name
                /bin/echo "Grabbing New Values"
                GatherData
                # 
                SectionEnd
                #
                /bin/echo "Checking New Values"
                Check
                #               
                SectionEnd
                #
                ScriptEnd
                #
                exit 0
                #
            else
                /bin/echo MATCH > /var/JAMF/Name+TargetName-Match.txt
                /bin/echo "Mac name already matches assigned name. Writing Marker File"
                /bin/echo "/var/JAMF/Name+TargetName-Match.txt"
                # 
                ScriptEnd
                #
                exit 0
        fi
    else
        #
        /bin/echo "Could not get assigned name from computer record"
        #
        ScriptEnd
        #
        exit 1
fi
#
ScriptEnd

jhuls
Contributor III

We use a product called Team Dynamix which serves as our inventory database among other things. In that database we have a field that contains what the name should be and have a scheduled script that exports that data as a csv with serial number along with what user account should get admin privileges on said machine. That csv sets on an smb file share that a script can then run from a policy after Jamf's enrollment phase on the computer that then configures the correct name and binds the machine to our domain and then gives the appropriate admin privileges if needed. The nice thing is that this solution can be used for both our Macs and Windows systems.

This solution works pretty well although the script needs some updating to account for certain scenarios. I am intrigued by the idea of preloading inventory though in the event that Apple ever blocks our ability to pull down our csv through a script.

alessio_tedesco
New Contributor III

Hi @Headbolt , this is really helpful but if you don't mind I would like to get in touch with you about some questions on this script, I'm kinda new to jamf and your help would be appreciated!