ihashacks

i dunno

Active Directory Ldapsearch Email List

I needed a sorted and formatted list of all Active Directory employees and contacts including their corresponding email. Here is what I came up with:

Query the domain:

1
ldapsearch -LLL -H ldap://ads.domain.com -x -b 'DC=ads,DC=domain,DC=com' -D 'DOMAIN\ldapbind' -W '(&(objectclass=person)(mail=*))' cn mail

Filter the output:

1
egrep ^'cn|mail'

Join matching cn and email pairs:

1
sed -e 'N;s/\n/ /'

Strip LDAP attribute labels and set field separator:

1
sed -e 'N;s/\n/ /' -e 's/cn:\ //' -e 's/\ mail:/,/'

Format and sort the output:

1
awk -F, ' { printf "%-25s %s\n",  $1, $2 } ' | sort

Final result:

1
2
3
4
5
6
ldapsearch -LLL -H ldap://ads.domain.com -x -b 'DC=ads,DC=domain,DC=com' \
-D 'DOMAIN\ldapbind' -W '(&(objectclass=person)(mail=*))' cn mail \
| egrep ^'cn|mail' \
| sed -e 'N;s/\n/ /' -e 's/cn:\ //' -e 's/\ mail:/,/' \
| awk -F, ' { printf "%-25s %s\n",  $1, $2 } ' \
| sort

Comments