Zimbra Bulk Account Creation.
Here’s the script that help me to create multiple account form csv file with format
“email,password,firstname,lastname,middlename”
#!/bin/bash
# Function to add a user to Zimbra using the provided parameters
function add_user() {
local email=$1
local password=$2
local firstname=$3
local lastname=$4
local middlename=$5
# Construct the Zimbra command using the provided parameters
zimbractl adduser -a $email -s $password -f "$firstname $lastname $middlename"
# Check if the command executed successfully and print a message accordingly
if [[ $? -eq 0 ]]; then
echo "User $email added successfully."
else
echo "Failed to add user $email. Please check the error message."
fi
}
# Check if a CSV file is provided as an argument
if [[ $# -ne 1 ]]; then
echo "Usage: $0 "
exit 1
fi
# Read the CSV file line by line
while IFS="," read -r email password firstname lastname middlename; do
# Skip the header row
if [[ "$email" == "email" ]]; then
continue
fi
# Call the add_user function with the read values
add_user "$email" "$password" "$firstname" "$lastname" "$middlename"
done < "$1"
Explanation:
-
Function Definition:
The
add_userfunction takes the email, password, first name, last name, and middle name as parameters.
It constructs the Zimbra command using the provided values and executes it.
The function checks the exit status of the command and prints an appropriate message. -
**Argument Validation:**The script checks if a CSV file is provided as an argument and exits if not.
-
CSV File Reading:
The script reads the CSV file line by line using the
while IFS="," read -r email password firstname lastname middlename; doloop.
TheIFSvariable is set to a comma to separate the fields in the CSV.
The-roption prevents backslashes from being interpreted as escape characters. -
Header Row Skipping:
The script checks if the current line is the header row and skips it if so.
-
User Addition:
The
add_userfunction is called with the values read from the CSV file to add the user.
To use this script:
- Save it as a Bash file (e.g.,
add_users.sh). - Make it executable:
chmod +x add_users.sh - Run the script under zimbra account
- Run it with the CSV file as an argument:
./add_users.sh your_csv_file.csv
Thanks