At the office, like many companies, we use Microsoft's Active Directory. As a Mac user and admin of some services it really helps to be able to work with Active Directory on a daily basis with Python.

I uploaded some scripts to my Github account to help out with that task. I've documented the code inside each script to get you up and running quickly. See the repo for more info.

utopiamachines/PythonActiveDirectory
Working with Microsoft Active Directory from Python - utopiamachines/PythonActiveDirectory

There are four scripts, but they work with two different Python modules.  The python-ldap module and the ldap3 module.  I use the ldap3 module as it has a nice interface for authenticating with NTLM and working with groups. I used these scripts as a basis to create some nice Lambdas in AWS at the office, maybe they will help you.

Scripts

get_ad_user_by_email.py

This script grabs a user's info from AD via their email attribute.

get_ad_user_by_ntid.py

This script grabs a user's info from AD via their NT ID [username].

get_ad_users_in_group.py

This script gets all the users in a group and prints them out.  I would export them to CSV, but I wanted to get the functionality documented. You can add that easily later on.

add_user_to_ldap_group.py

This one uses the ldap3 module to add a user to the specified group.  


My blog posts tagged with "Python Patterns" are designed to be a quick look reference for some Python code snippets I use a lot.  They are written to be a quick starting point for future projects so I do not need to type as much.