# Sync Device Name

> macOS script that reads the local ComputerName and updates the device display name in the Applivery Dashboard via API, keeping all device names in sync.

Source: https://docs.applivery.com/en/device-management/apple/macos/scripts/sync-device-name/  •  Last updated: 2026-04-18

**Key topics:** device management, automation, scripting, Applivery, MDM, IT administrators

---

**TL;DR:** Automate repetitive tasks on managed devices using scripts in Applivery for efficient device management.

Every Mac has a local computer name — the one the user set during initial setup, or changed later to something personal like "John Doe's MacBook". In the Applivery Dashboard, however, Devices typically show the generic name assigned at enrollment time. At scale, this makes identifying specific machines by name nearly impossible: you end up with a list of `MacBook-Pro-7` entries and no easy way to know who each one belongs to.

This Script closes that gap. It reads the local `ComputerName` from each Mac and updates it in the Applivery Dashboard via the API, keeping both in sync. Run it periodically, and you can always identify any Device at a glance.

:::warning
The Applivery Agent for macOS must be installed and active on the Device. Learn more about the [macOS Agent](https://docs.applivery.com/en/device-management/apple/apple-policies/agent/#agent-app-for-macos-devices).
:::

## Requirements

| Requirement | Detail |
| --- | --- |
| Platform | macOS |
| Execution privileges | Root (default in Applivery) |
| Internet access | The Device must be able to reach `api.applivery.io` |
| Bearer Token | A valid token from a Service Account with Editor role or above |
| Organization ID | The slug/ID of your Applivery organization |

* * *

## Before you start — Get your Bearer Token and Organization ID

**Organization ID** — Your Organization ID is the slug visible in the Applivery Dashboard URL (e.g., `my-company`).

**Bearer Token (via Service Account)** — Applivery uses [Service Accounts](https://docs.applivery.com/en/platform/api/service-accounts/) to represent non-human users that can access the Applivery Administration API. Each Service Account has an associated Bearer token used as the `Authorization` header in API calls.

* * *

## Setup

**Create the Script**

Once in the [**Applivery Dashboard**](https://dashboard.applivery.io/), follow the steps described [here](https://docs.applivery.com/en/device-management/apple/macos/scripts/index) to create a Script. Paste the following Script into the editor, replacing the placeholder values shown in the table below.

| Variable | Description | Example |
| --- | --- | --- |
| `ID_ORG` | Your Applivery organization slug/ID | `my-company` |
| `API_TOKEN` | Bearer token from a Service Account with Editor role or above | `wfkj2Gi...` |

Select **Bash** as the language, give it a descriptive name (e.g., `Sync Device Name`), and click **Create**.

```bash
#!/bin/bash

# ---
# Title: Sync Device Name with Applivery API
# Description: Automatically updates the Device Display Name in Applivery Dashboard to match the local macOS ComputerName.
# Author: Applivery
# Version: 1.0.0
# ---

# ==========================================
# CONFIGURATION
# ==========================================
ID_ORG="your-org-id"
API_TOKEN="your-api-token"
BASE_URL="https://api.applivery.io/v1/organizations/$ID_ORG/mdm/apple/enterprise/devices"

# ==========================================
# 1. LOCAL DATA GATHERING
# ==========================================
SERIAL_NUMBER=$(system_profiler SPHardwareDataType | grep "Serial Number" | awk '{print $4}')
NEW_DEVICE_NAME=$(scutil --get ComputerName)

if [ -z "$NEW_DEVICE_NAME" ]; then
  echo "Error: Local ComputerName is empty. Please set a hostname on the Mac."
  exit 1
fi

echo "Syncing Device Name for Serial: $SERIAL_NUMBER"
echo "Target Name: $NEW_DEVICE_NAME"

# ==========================================
# 2. API INTERACTION
# ==========================================

# GET Device Info to find the internal Device ID
response=$(curl -s -X GET "$BASE_URL/$SERIAL_NUMBER" \
  -H "Authorization: Bearer $API_TOKEN" \
  -H "Content-Type: application/json")

if echo "$response" | grep -q '"admDevice"'; then
  DEVICE_ID=$(echo "$response" | grep -o '"admDevice":"[^"]*"' | sed 's/"admDevice":"\([^"]*\)"/\1/')

  if [ -n "$DEVICE_ID" ]; then
    echo "Found internal DeviceID: $DEVICE_ID"

    update_response=$(curl -s -X PUT "$BASE_URL/$DEVICE_ID" \
      -H "Authorization: Bearer $API_TOKEN" \
      -H "Content-Type: application/json" \
      -d "{\"displayName\": \"$NEW_DEVICE_NAME\"}")

    if echo "$update_response" | grep -q '"status":200' || echo "$update_response" | grep -q '"displayName"'; then
      echo "SUCCESS: Display name updated to: $NEW_DEVICE_NAME"
    else
      echo "FAILURE: Error updating name: $update_response"
    fi
  else
    echo "ERROR: Could not find DeviceID for Serial Number $SERIAL_NUMBER"
  fi
else
  echo "ERROR: Could not fetch device info from API. Check Token and Org ID."
  echo "Response: $response"
fi

exit 0
```

**Assign the Script to a Device**

Now, navigate to any of your **Devices**, select the **Scripts** tab, click on the **\+ Assign Script** button, and select the one you just created.

:::info
You can also assign Scripts to Policies. To do this, navigate to the **Policies** section, select the desired Policy, and click on the **Scripts** tab. The process will be the same as when assigning it directly to an individual Device.
:::

**Choose the execution method**

| Method | Behaviour | Recommended? |
| --- | --- | --- |
| **Once** | Runs one time per Device. | ✅ Useful for setting the display name from day one of enrollment. |
| **Loop** | Runs repeatedly at the configured interval (15m, 1h, 6h, 1d, 7d). | ✅ Recommended for ongoing sync — keeps the Dashboard updated as users rename their machines. |
| **On demand** | Only runs when manually triggered. | ✅ Useful for an ad-hoc update triggered by IT. |

The recommended setup is to use **Loop** with a weekly interval (`7d`) to keep the Dashboard in sync without generating unnecessary API traffic, combined with **Once** during enrollment to set the name from the first day.

This Script does not require any arguments. The Device name and serial number are obtained automatically from the local system. Click **Add** to save the assignment.

* * *

## Available on GitHub

This Script is part of the [Applivery Public Script Repository](https://github.com/applivery/applivery-mdm-scripts/tree/main/Apple/Sync%20Device%20Name%20%28API%29). The full source code is available there for review, adaptation, and contribution.
