Jamf Variables ($BUILDING, $ROOM) as Script Parameters ($4, $5)

dungeonadept
New Contributor III

I'm looking to create a hostname renaming script for Macbooks deployed across various buildings and sites, and I was thinking I'd be able to use the variables from Jamf to define part of the hostname.

Setting the parameter as $BUILDING doesn't seem to work, it just passes through as "$BUILDING". Is there a way to make this work, or will I have to create a separate profile for each site and assign them to the appropriate Jamf group?

3 REPLIES 3

matthias_bretz
New Contributor III

I would like to do something very similar but running into the same issues. Do the variables as described here (https://docs.jamf.com/10.21.0/jamf-pro/administrator-guide/Computer_Configuration_Profiles.html) only work in config-profiles and not as input for policy-scripts?
I am trying to use $DEPARTMENTNAME, $ROOM and $FULLNAME

Andreas_Schenk
Contributor

I think that this can be done, but not too easly, as it involves using the API.
I have used the following script a few times. It sets the name to the asset tag. It could be easily adapted to use the Room or Department or any other value.
Of course, it requires you to set up an API User with privs to read computer objects.

Here is the script:

#!/bin/sh

# vars
SERIAL_NUMBER=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
JSS="https://my_server.jamfcloud.com"
API_USER="$4"
API_PASS="$5"
ASSET_TAG_INFO=$(curl -k $JSS/JSSResource/computers/serialnumber/$SERIAL_NUMBER --user $API_USER:$API_PASS | xmllint --format --xpath '/computer/general/asset_tag/text()' -)

SUFFIX=""

if [ -n "$ASSET_TAG_INFO" ]; then
    echo "Processing new name for this client..."
    echo "Changing name..."
    jamf -setComputerName -name "$ASSET_TAG_INFO"$SUFFIX
    echo "Name change complete. ("$ASSET_TAG_INFO"$SUFFIX)"

else
    echo "Asset Tag information was unavailable. Using Serial Number instead."
    echo "Changing Name..."
    jamf -setComputerName -name $SERIAL_NUMBER$SUFFIX
    echo "Name Change Complete ($SERIAL_NUMBER$SUFFIX)"

fi

matthias_bretz
New Contributor III

Great, below is what I made of it.
In general this works great but "jamf setComputerName -name" doesn't set the .local-name on my Macs.

#!/bin/zsh
local SERIAL_NUMBER
local JSS
local API_USER
local API_PASS
local MODELNAME
local DEPARTMENT
local DEVICE
local ROOM
local NAME
local MAC_NAME

# vars
SERIAL_NUMBER=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F" '/IOPlatformSerialNumber/{print $(NF-1)}')
MODELNAME=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ': ' {'print $2'})
if [[ -n $4 ]] ; then
  JSS="$4"
else
  echo "Jamf-URL not set."
  exit 1
fi
if [[ -n $5 ]] ; then
  API_USER="$5"
else
  echo "API-User not set."
  exit 1
fi
if [[ -n $6 ]] ; then
  API_PASS="$6"
else
  echo "API-Password not set."
  exit 1
fi

case $MODELNAME in
    *Pro*)
        DEVICE="MB Pro"
    ;;
    *Air*)
        DEVICE="MB Air"
    ;;
    *mini*)
        DEVICE="Mac mini"
    ;;
    *iMac*)
        DEVICE="iMac"
    ;;
    *)
        DEVICE="unknownMac"
    ;;
esac

DEPARTMENT=$(curl -k $JSS/JSSResource/computers/serialnumber/$SERIAL_NUMBER --user $API_USER:$API_PASS | xmllint --format --xpath '/computer/location/department/text()' -)
ROOM=$(curl -k $JSS/JSSResource/computers/serialnumber/$SERIAL_NUMBER --user $API_USER:$API_PASS | xmllint --format --xpath '/computer/location/room/text()' -)
NAME=$(curl -k $JSS/JSSResource/computers/serialnumber/$SERIAL_NUMBER --user $API_USER:$API_PASS | xmllint --format --xpath '/computer/location/username/text()' -)
NAME=$(echo $NAME | awk -F '.' '{print $1 " " $2}' | awk 'NF>1{print $NF}')
NAME=${(C)NAME}

MAC_NAME=$DEPARTMENT" "$DEVICE" "$ROOM" ("$NAME")"
jamf setComputerName -name $MAC_NAME
sleep 1
jamf recon
exit 0