What It Does
This script changes nothing until you set DRY_RUN=false and APPROVE=set-mac-local-admin-password. Each successful real run sets a new random password and replaces the previous value in the field.
For ACCOUNT_NAME (mspadmin by default), it creates the account as a hidden administrator if it does not exist: a user ID below 500 so it is not shown at the login window or in System Settings, IsHidden set, a private home folder under /var, and membership of the admin group. If the account already exists, the script refuses the signed-in console user's account and any account with a user ID of 500 or higher that lacks the hidden flag. For other existing accounts, it sets the hidden flag and adds admin membership when needed, then sets a new password. The script does not check who created the account.
The password is generated from /dev/urandom (PASSWORD_LENGTH characters, with at least one upper case letter, lower case letter, digit and symbol). It goes to dscl on its standard input and to the NinjaOne field through ninjarmm-cli on its standard input, never as a command-line argument. It is never printed, logged, exported or put in the answer. RESULT_FIELD is required here and must be a secure field. Without it, with OUTPUT_FORMAT Answer, or where ninjarmm-cli is not found, the script ends in ERROR before changing anything. If the field write fails after the password changed, the run ends in ERROR and the password is lost, so run it again.
An account made this way has no SecureToken. macOS gives one only when an existing SecureToken administrator's credentials are supplied, and a root script has none. Such an account can administer the Mac and sign in over SSH or at a text login, but it cannot unlock FileVault at start-up and, on Apple silicon, cannot authorize some macOS updates. Old password hashes are not backed up, and services that logged in with the old password stop working.
Before You Run It
- Create a secure (secret) custom field in NinjaOne first and give its name as RESULT_FIELD.
- Run it once as a dry run and read what it would change.
- Decide whether you need a SecureToken. If you do, grant it by hand, because this script cannot.
- Find the services and tools that use the old password. They stop working when it rotates.
- Use an ACCOUNT_NAME reserved for the managed admin. An eligible existing account can be hidden or added to the admin group and have its password changed.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: High. It is a dry run unless you set both DRY_RUN=false and APPROVE=set-mac-local-admin-password, and it stops before making more than 25 changes in one run unless you raise MAX_CHANGES. Run it once as a dry run and read the list first.
- What it changes: Creates a hidden local administrator account if missing, sets a new random password on it, writes the password to a NinjaOne secure field.
- Restart: It never restarts the machine.
- Typical run: Under 10 seconds.
- Example answer: "Rotated password for hidden admin mspadmin; password written to field MacAdminPassword"
- Tested before listing on macOS 27 (Apple silicon VM) with /bin/bash 3.2, run as root, against its own test cases.
Inputs
Pass an input as an argument (--input-name value), as an environment variable named as listed, which is how most RMMs pass script variables, or change its default in the DEFAULT_ lines near the top of the script. An argument wins over an environment variable, which wins over the default.
- ACCOUNT_NAME: Short name of the managed admin. Letters, digits, . _ -; starts with a letter or underscore. Default mspadmin.
- PASSWORD_LENGTH: Length of the generated password. 16-64. Default 24.
- OUTPUT_FORMAT: Text (default), Json or Csv. Answer is refused (this script produces a secret).
- EXPORT_PATH: Also write rows (never the password) to this .csv or .json file.
- RESULT_FIELD: REQUIRED: the NinjaOne secure custom field that receives the password.
Every script also takes DRY_RUN, OUTPUT_FORMAT (Text, Answer, Json or Csv), EXPORT_PATH and RESULT_FIELD, which writes the answer to an RMM custom field, unless the list above words one differently.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.