Skip to main content

Execute macOS scripts as Defined User

Description

By default, the FileWave Client executes scripts and tasks with elevated permissions (root on macOS). ThisThe additionbelow shows a method to the start oflaunch a script will execute itcommand as thean currently logged-inalternate user (alsowhen knownran asthrough the console user).FileWave.

Ingredients

  • Text editor
  • FileWave Central

Directions

    The

  1. Insertsudo thiscommand nearmay be used to define a user to run a command.  Launchctl may also be used to define a user.  In some instances only one of these options may be successful.  However, both may be defined in the startsame command at the same time, increasing the chances of yoursuccess.

    script,

    The beforebelow method not only shows a method to define the desireduser, actions.but

    #!/bin/zshgrabs current_user=$(statthe -f%Sucurrent /dev/console)'console' current_user_id=$(iduser.  -u $current_user)
    Example:

    #!/bin/zsh
    current_user=$(stat -f%Su /dev/console)
    current_user_id=$(id -u $current_user)
    
    whoami
    
    launchctl asuser $current_user_id sudo -u $current_user whoami
    echo "hello world" >> /Users/$current_user/Desktop/test.txt
    
  2. When

ran through FileWave, the output of the above will show the output of the 'whoami' command has altered, by first echoing the root username and then the name of the active console user of the device.

If the current user logged in where 'sholden', the output should show:

root
sholden