I'm running the below script. (taken from a post on this site). Everything seems to execute ok, all the emails I wanted to create are created, but there is an error causing an inaccurate report. I've narrowed the problem down to the line:
Code:
${ZIMBRADIR}/bin/zmprov -l getAllAccounts ${MYDOMAIN} > ${ZMUSERS}
ZIMBRADIR="/opt/zimbra"
MYDOMAIN = [my domain name]
ZMUSERS = "/var/temp/import-ad-zmuserlist.$$"

The above code is not pulling all of the users account. When the script determines which accounts are not already present, it thinks none of the accounts are present so it tries to create them all over again. In the end it does work, zmprov createAccount simply errors on the account that already exist, but then the scripts ends before it should and the email generated with the created accounts is inaccurate.

/var/scripts/common/standard.conf
Code:
## Global Variables ##
TEMPDIR="/var/temp"
ZIMBRADIR="/opt/zimbra"
SHAREDIR="/srv/samba/share"
MYDOMAIN="mydomain.edu"
ADMINEMAIL="admin@${MYDOMAIN}"
REPORTEMAIL="gene@${MYDOMAIN}"
BACKUPDIR="/var/backup"
OFFSITEDIR="/mnt/zmbackup"
OFFSITETESTFILE="${OFFSITEDIR}/online.txt"
ARCHIVEMETHOD="tar.7z"    ## Choices are tar.7z or tgz
HOSTNAME=$(hostname -s)
SCRIPTNAME=$0
MAILFILE="${TEMPDIR}/mailfile.$$"

## Global Functions ##

function f_sendmail()
{
  ## Purpose: Send email message.
  ## Parameter #1 = Subject
  ## Parameter #2 = Body
  echo "From: ${ADMINEMAIL}" > ${MAILFILE}
  echo "To: ${REPORTEMAIL}" >> ${MAILFILE}
  echo "Subject: ${1}" >> ${MAILFILE}
  echo "" >> ${MAILFILE}
  echo -e ${2} >> ${MAILFILE}
  echo "" >> ${MAILFILE}
  echo -e "\n\nServer: ${HOSTNAME}\nProgram: ${SCRIPTNAME}\nLog: ${LOGFILE}" >> ${MAILFILE}
  ${ZIMBRADIR}/postfix/sbin/sendmail -t < ${MAILFILE}
  rm ${MAILFILE}
}

function f_mount()
{
  ## Mount the pre-configured Windows share folder.
  ## NOTE: The Windows share should have a file called "online.txt"
  if [ ! -f ${OFFSITEDIR}/online.txt ]; then
    mount -t cifs //kdc-doubletake/zimbra ${OFFSITEDIR} --options nouser,rw,nofail,noatime,noexec,credentials=/etc/cifspw
  fi
}

function f_umount()
{
  ## Dismount the Windows share folder.
  ## NOTE: The unmounted folder should have a file called "offline.txt"
  if [ -f ${OFFSITEDIR}/online.txt ]; then
    umount ${OFFSITEDIR}
  fi
}
Script I'm running
Code:
#!`/bin/bash
###########################################
## Name          : Email-List.ba
## Version       : 1.2
## Date          : 2011-11-02
## Author        : LHammonds
## Purpose       : Add mailbox accounts for AD users that have none.
## Compatibility : Verified on Ubuntu Server 10.04.3 LTS, Zimbra 7.1.2 - 7.1.3 OSE
## Requirements  : Zimbra must be online, must be run as root user.
## Run Frequency : Run very frequently (every minute)
## Exit Codes    :
##    0 = Success
##    1 = Failure
################ CHANGE LOG #################
## DATE       WHO WHAT WAS CHANGED
## ---------- --- ----------------------------
## 2011-10-11 LTH Created script.
## 2011-10-29 LTH Better logging, mail support, bug fixes.
## 2011-11-02 LTH Moved standard variables/functions to external file.
############################################

## Import standard variables and functions. ##
source /var/scripts/common/standard.conf

LOGFILE="${TEMPDIR}/import-ad.log"
MAILFILE="${TEMPDIR}/import-ad-mail.$$"
ADLISTORG="${SHAREDIR}/CSVExport.csv"
ADLISTNEW="${TEMPDIR}/import-ad-adlist.$$"
ZMUSERS="${TEMPDIR}/import-ad-zmuserlist.$$"
ZMCMD="${TEMPDIR}/import-ad-zmcmd.$$"
CURUSERS="/var/temp/UserList.$$"
NEWUSERS=""
RETURNVALUE=0

# Temporarily change the default field separator to a comma. #
OLDIFS="${IFS}"
IFS=","


# If the Active Directory user list file is not found, exit the script. #
if [ ! -f ${ADLISTORG} ]; then
  ## No file to process.  Exit script.
  echo "no file found" >> ${LOGFILE}
  exit 0
fi
echo "`date +%Y-%m-%d_%H:%M:%S` - Active Directory file found! Import process started." >> ${LOGFILE}

## Create files and set permissions for only the root user. #
touch ${ZMCMD}
touch ${ZMUSERS}
touch ${CURUSERS}
chmod 0600 ${ZMCMD}
chmod 0600 ${ZMUSERS}
## Take ownership and set permissions for only the root user. #
chown root:root ${ADLISTORG}
chmod 0400 ${ADLISTORG}
## Move the file to a working folder. #
cp ${ADLISTORG} ${ADLISTNEW}
## Create a list of users in Zimbra for the specified domain. #
${ZIMBRADIR}/bin/zmprov -l getAllAccounts ${MYDOMAIN} > ${ZMUSERS}



## Loop through the Active Directory user list. #
while read -a ADLINE ; do
  ## Slot 0 = UserID
  ## Slot 1 = First Name (GivenName)
  ## Slot 2 = Initials
  ## Slot 3 = Last Name (sn)
  ## Slot 4 = Full Name (CN)
  ## Slot 5 = Title
  ## Slot 6 = Description
  ## Slot 7 = Comment
  ## Slot 8 = Telephone / Work Number
  ## Slot 9 = Home Phone
  ## Slot 10 = Mobile Phone
  ## Slot 11 = Fax Number (facsimileTelephoneNumber)
  ## Slot 12 = Pager
  ## Slot 13 = Company
  ## Slot 14 = Office (physicalDeliveryOfficeName)
  ## Slot 15 = Street Address
  ## Slot 16 = Post Office Box
  ## Slot 17 = City (l)
  ## Slot 18 = State (st)
  ## Slot 19 = Postal Code
  ## Slot 20 = Country Code (co)
  ## Slot 21 = Password
  ## Set the "Found Match" variable to false #
  FOUND=0
  ## Loop through the Zimbra User List. #
  while read -a ZMLINE ; do
    ## Convert Usernames to lower case. #
    ZMUSERID=${ZMLINE[0],,}
    ADUSERID=${ADLINE[0],,}
    ## Add domain address to the end of the AD Username #
    ADUSERID=${ADUSERID}"@"${MYDOMAIN}
    echo "${ZMLINE[0]}">>${CURUSERS}
    ## Compare the lowercase AD username to the lower case Zimbra username. #
    if [ ${ADUSERID} == ${ZMUSERID} ]; then
      ## Match found. Exit this while loop. #
      FOUND=1
      break
    fi
  done < ${ZMUSERS}

  if [ ${FOUND} -eq 0 ]; then
    ## We have an AD user that does not have a Zimbra mailbox. #
    NEWUSERS="${NEWUSERS}${ADLINE[0]}@${MYDOMAIN}\n"
    echo "`date +%Y-%m-%d_%H:%M:%S` --- Adding new user: ${ADLINE[0]}@${MYDOMAIN}" >> ${LOGFILE}
    echo "createAccount ${ADLINE[0]}@${MYDOMAIN} \"${ADLINE[21]}\" gn \"${ADLINE[1]}\" initials \"${ADLINE[2]}\" sn \"${ADLINE[3]}\" cn \"${ADLINE[4]}\" title \"${ADLINE[5]}\" description \"${ADLINE[6]}\" zimbraNotes \"${ADLINE[7]}\" telephoneNumber \"${ADLINE[8]}\" homePhone \"${ADLINE[9]}\" mobile \"${ADLINE[10]}\" facsimileTelephoneNumber \"${ADLINE[11]}\" pager \"${ADLINE[12]}\" company \"${ADLINE[13]}\" physicalDeliveryOfficeName \"${ADLINE[14]}\" street \"${ADLINE[15]}\" postOfficeBox \"${ADLINE[16]}\" l \"${ADLINE[17]}\" st \"${ADLINE[18]}\" postalCode \"${ADLINE[19]}\" co \"${ADLINE[20]}\"">> ${ZMCMD}
  fi
done < ${ADLISTNEW}
echo "end of write file"

## If the Zimbra command file is not empty, we need to process it. #
if [ -s ${ZMCMD} ]; then

  ${ZIMBRADIR}/bin/zmprov < ${ZMCMD} 1>/dev/null 2>&1
  RETURNVALUE=$?
  if [ ${RETURNVALUE} -ne 0 ]; then
    ## Something went wrong with the mailbox creation.
    ## This error should notify administrators to the problem.
    echo "`date +%Y-%m-%d_%H:%M:%S` --- ERROR: zmprov reported the following error: ${RETURNVALUE}" >> ${LOGFILE}
    echo "`date +%Y-%m-%d_%H:%M:%S` - Import aborted.  EXIT CODE = 1" >> ${LOGFILE}
    f_sendmail "Zimbra Active Directory Import" "ERROR: zmprov reported the following error: ${RETURNVALUE}\n\nRemaining temporary files:\n${ZMUSERS}\n${ZMCMD}\n${ADLISTNEW}\n"
    exit 1
  fi
## Send email notification of the new users added to Zimbra. #
  ## NOTE: To be 100% sure about this, we could loop through the users #
  ##       and verify that they now have Zimbra mailboxes. #
  f_sendmail "Zimbra Notification - New Users" "The following users were added to Zimbra:\n\n${NEWUSERS}"
else
  ## There were no new users to process. #
  echo "`date +%Y-%m-%d_%H:%M:%S` --- No new users to import." >> ${LOGFILE}
fi
echo "`date +%Y-%m-%d_%H:%M:%S` - Active Directory import completed." >> ${LOGFILE}

## Restore the default field separator value. #
IFS="${OLDIFS}"

## Remove temporary files. #
##rm ${ZMUSERS}
##rm ${ZMCMD}
##rm ${ADLISTNEW}