Mac Bash Worklet to Copy Files to Users' Home Folders

Objective

To provide a reusable macOS Worklet script that identifies active local user accounts on macOS endpoints and copies target payload files into each user's /Users home directory with correct ownership permissions.

Overview

Because the Automox Agent executes scripts under the local root (NT AUTHORITY\SYSTEM equivalent) context, files uploaded or generated during Worklet execution belong to root by default.

When deploying user-level configurations (such as desktop backgrounds, configuration plists, dotfiles, or application templates), scripts must loop through all local user profiles, copy the files, and update file ownership (chown) to match each respective user account.

Script Configuration Variables

Before deploying the Worklet, customize the script variables in the Remediation Code block:

Variable NameDescription & Example Value
UHOMEBase path for user home directories. Defaults to "/Users".
_FILESSpace-separated list of local file paths to copy (e.g., "./config.plist ./background.png").

Worklet Setup Instructions

 

1.Create a New macOS Worklet:

  1. In the Automox console, navigate to Policies > Create Policy.
  2. Select Worklet and set the OS target to macOS.
  3. Enter a descriptive policy name (e.g., macOS - Distribute Configuration Files to User Home Directories).

2.Upload Payload Files:

In the Payload section of the Worklet creation page, upload the file(s) you intend to distribute to target endpoints. Uploaded files are placed in the current working directory (./) during execution.

3.Configure Evaluation Code:

To ensure file distribution runs on demand or during scheduled maintenance cycles, set the Evaluation Code to return exit code 1 (Non-Compliant):

#!/bin/bash
# Force remediation execution during policy cycle
exit 1

4.Configure Remediation Code:

Paste and customize the following script in the Remediation Code block:

#!/bin/bash
# Automox Worklet: Copy files to all local user home directories

# Base user directory path
UHOME="/Users"

# Space-separated list of uploaded payload files to copy
_FILES="./test_file ./test_file2"

# Get a list of all human user accounts (excluding system accounts starting with '_')
_USERS="$(dscl . list /Users | grep -v '^_')"

# Iterate through user accounts and copy files
for u in $_USERS; do
    # Exclude Shared directory or daemon users
    if [ "$u" = "Shared" ] || [ "$u" = "daemon" ] || [ "$u" = "nobody" ]; then
        continue
    fi

    _dir="${UHOME}/${u}"

    # Verify home directory exists before copying
    if [ -d "$_dir" ]; then
        for f in $_FILES; do
            if [ -f "$f" ]; then
                /bin/cp -f "$f" "$_dir/"
                file_name=$(basename "$f")
                
                # Set ownership to match the local user account
                user_uid=$(id -u "$u" 2>/dev/null)
                user_gid=$(id -g "$u" 2>/dev/null)
                
                if [ -n "$user_uid" ] && [ -n "$user_gid" ]; then
                    chown "${user_uid}:${user_gid}" "$_dir/$file_name"
                    echo "Successfully copied $file_name to $_dir"
                fi
            fi
        done
    fi
done

exit 0

5.Assign Device Groups & Execute:

Assign the Worklet policy to your target macOS Device Groups and run the policy or save it to your schedule.

 

Key Operational Notes

  • User Filtering: The script uses dscl . list /Users while filtering out system accounts (_), the /Users/Shared directory, and system daemons to ensure files are only copied to actual user profiles.

  • Permissions Enforcement: Using id -u and id -g ensures the copied files are explicitly reassigned from root to the target user's UID and primary GID.

Was this article helpful?
0 out of 0 found this helpful