Invite Users from CSV - Bash Script

Hi all,

I’m the API Tech Docs Writer here at Automox, and I wanted to introduce myself and let you know I’m here for any API-related questions you might have. To kick things off, here’s a quick Bash script I created.

This script will allow you to invite a list of users and assign roles from a CSV to a given Automox organization. This is particularly useful in cases where you might have a number of users that need to be added, like when onboarding new employees.

This script is org specific, so if you have multiple orgs, you will need to create a separate CSV for each org, containing the users to be added to that org and their roles. You would then need to run the script against each org, using the specific org id and api key.

  1. Copy the script, and save it as adduser_csv.sh in the directory of your choice. Use chmod u+x adduser_csv.sh to change the permissions of the file to make it executable.

  2. Create a CSV file containing 2 columns. The headers for these columns should be Email and RBAC_Role. Add the email addresses of the users you want to add to the Email column, and the role for each of them in the RBAC_Role column. Save this file to the same directory where you have the script stored.

    • Acceptable values for RBAC_Role are as follows:
      • Full Administrator
      • Patch Administrator
      • Billing Administrator
      • Read Only

    Note: When creating your CSV file, be sure that the file conforms to the following specifications (You can check this by opening the file in a text editor)

    • Your CSV should contain a header. This script is set to start from row 2, so it will ignore the header. If your CSV does not contain a header, it will skip the user on the first line.
    • Each line should end with a LF (not CR or CRLF).
    • Fields should be delimited by a comma, and text should not be quoted.
    • Column 1 should contain valid email addresses for the users you would like to invite.
    • Column 2 should contain an acceptable value for RBAC_Role for each user.
  3. Open a terminal, change to the directory where you’ve saved the script and where the CSV file is located, and run the script by entering ./adduser_csv.sh. The script will prompt you for the name of your CSV file, your API key, and the organization ID.

    • Note: You must use the terminal to run this script.

When successful, this script will return an object for each invited user, and an HTTP code, in this case, a 200.

If there is invalid data (an invalid email or an unacceptable value for RBAC_Role), the script will return which row contains invalid data and continue acting on the remaining rows.

Example:

   {"inviter_firstname":"Laurie","inviter_lastname":"Laidley","email":"test@example.com","org_name":"My Automox Test Org"}200

Script:

#!/bin/bash
read -r -p "Please enter your organization ID:" org < /dev/tty
read -r -p "Please enter the name of your CSV file:" filename < /dev/tty
read -r -s -p "Please enter your API key:" apiKey < /dev/tty
printf "\n"
i=1
declare -a rbac_array=(
    'Full Administrator'
    'Patch Administrator'
    'Billing Administrator'
    'Read Only'
    )

pattern="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"

while IFS="," read -r email rbac_role; do
    if [[ "$email" =~ $pattern ]]; then
        valid_email=1
    else
        valid_email=0
    fi

    for value in "${rbac_array[@]}"; do
        if [[ "$rbac_role" = "$value" ]]; then
            valid_role=1
            break
        else
            valid_role=0
        fi
    done

    if [[ ${valid_email} -eq 1 ]] && [[ ${valid_role} -eq 1 ]]; then
        curl -s -o /dev/tty \
        -w "%{http_code}" \
        --location \
        --request POST "https://console.automox.com/inviteUser?o=${org}&email=${email}&rbac_role=${rbac_role}" \
        --header "Content-Type: application/json" \
        --header "Authorization: Bearer ${apiKey}"
        printf "\n"
    else
        echo "CSV row ${i} contains invalid data!"
    fi

done <<< "$(tail -n +2 "${filename}")"

6 Likes

When running your script in wsl terminal (not in vscode) I get the following error:

    : No such file or directorydev/tty
    : No such file or directorydev/tty
    : No such file or directorydev/tty

    ./adduser_csv.sh: line 13: $'\r': command not found
    ./adduser_csv.sh: line 15: $'\r': command not found
    ./adduser_csv.sh: line 23: syntax error near unexpected token `$'do\r''
    '/adduser_csv.sh: line 23: `    for value in "${rbac_array[@]}"; do

Not sure what’s going on. Added u+x and I own the file.

Hi driosk,

This script was created using Bash, and was tested with Bash and Zsh. I don’t think it will work correctly in a wsl terminal. You would need to install Bash or Zsh, and use one of those to run the script.

1 Like

10-4. I think I have it installed anyway. :slight_smile: I’ll test in bash. Thank you!

Edit: Yep that did the trick! Using gitbash, do you foresee that causing any issues?
image

I think gitbash should be ok, however I haven’t tested the script using that version of Bash so YMMV. Please let me know if you run into any issues, so I can look into it. :slightly_smiling_face:

Hoping to have a version of this script available for PowerShell users soon.

1 Like