Advertisement

08.08.2008 at 10:48AM PDT, ID: 23633491 | Points: 500
[x]
Attachment Details

DirectoryEntry.Invoke giving COMException for SetPassword when creating a new user

Asked by TimMullin in Active Directory

Tags: Microsoft, Active Directory, Server 2008

I am trying to create a new AD user using the DirectoryEntry class in C#.  I create an OU, then I create the user (via container.Children.Add()), set some propertys, commit, then try to invoke "SetPassword".  Invoking "SetPassword" is generating the following exception:
System.Runtime.InteropServices.COMException (0x80020006): Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
   at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object args)

Curiously, if I delete the OU, and run my code again, then everything works just fine.  I don't know why it always give an exception the first time I run the block of code.

I am testing on a virtual machine so I can revert to a snapshot that I took before running my test.
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
DirectoryEntry NewUser = contain.Children.Add("cn=NewUser", "MyCustomSchema");
NewUser.Properties["sAMAccountName"].Add("NewUser");
NewUser.Properties["Prop1"].Value = "*";
NewUser.CommitChanges();
 
// This line crashes the first time around
NewUser.Invoke("SetPassword", new object[] { "password" });
 
// Finish setting the user properties and commits
[+][-]08.08.2008 at 04:30PM PDT, ID: 22194324

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628