[python-win32] Active Directory group creation

Tim Golden mail at timgolden.me.uk
Tue Jan 15 10:09:16 CET 2013


On 14/01/2013 17:49, Trevor Rowland wrote:
> Has anyone managed to create an Active Directory group using either
> win32com.client or active_directroy modules ? if so how?
> 
> I have a script automatically adding users to active directory groups
> when they are added to projects in our project management system, but
> ideally I would like to automatically create groups when they are
> created in the same system


It's not too hard to do it with native win32com:

<code>
import win32com.client

#
# Adjust to suit, obviously...
#
dn = "OU=Test,OU=Camden,DC=XX,DC=XX,DC=XX"

ou = win32com.client.GetObject("LDAP://%s" % dn)

group = ou.Create("group", "cn=testgroup")
group.displayName = "Test Group"
group.SetInfo()

</code>

If you're already using the active_directory module anyway, the current
git version [*] (note to self: make a new release) offers a bit of
syntax sugar here:

<code>
import active_directory

dn = "OU=Test,OU=Camden,DC=XX,DC=XX,DC=XX"
ou = active_directory.AD_object("LDAP://%s" % dn)

ou['cn=testgroup2'] = dict(Class="group", displayName="Test Group2")
group = ou['testgroup2']

#
# or
#
group = ou.add(
  "cn=testgroup3",
  Class="group",
  displayName="Test Group3"
)

</code>

The __setitem__ approach is a little clunky; it's there more to be
parallel with its slightly easier __getitem__ and __delitem__ siblings.

TJG

[*] https://github.com/tjguk/active_directory


More information about the python-win32 mailing list