A previous post of mine that covered LDAP searches in non-Microsoft Active Directory LDAP directories (ex. OpenLDAP, Oracle Internet Directory, Novell eDirectory, etc.) gained a fair amount of popularity and I received requests to follow up on it to provide instructions on performing other LDAP functions using the .Net Framework and PowerShell.
Continuing from my last post, I went ahead and set up an OpenLDAP server for demonstration purposes utilizing the core, cosine, and inetorgperson schemas. I have prepopulated it with the following objects:
o: Mikes Technology Blog
description: My first test OU
description: My second test OU
In this example, we will modify the testuser user in testou1 to be contained in a new container (ou=testou2,dc=mikesblog,dc=lan). Optionally, we can also rename the user (I show the field below, but don't actually rename the user).
To accomplish this, we will use the .Net framework classes in the System.DirectoryServices.Protocols namespace. The LdapConnection class gives our application connectivity to the LDAP server where we can provide an ModifyDNRequest object and receive an ModifyDNReply.
The sample code below is designed to demonstrate how to move or rename an LDAP entry, in practice this might be designed to use the command pattern instead of the hardcoded transaction script below.
Let's get to it... The sample C#.Net code:
Not to disappoint, below is the PowerShell port of the above code.