Question Populating AD with users - error setting password

thuhreb

New member
Joined
May 21, 2010
Messages
1
Programming Experience
1-3
For some reason, the code below will set the password for the first user it adds, and creates the second user, but bombs out on setting the second user's password. The error detail is directly below then the code starts. I've tried using both
NewUser.NativeObject.setpassword("testPW")
and
NewUser.Invoke("SetPassword", "testPW)

Any help would be greatly appreciated.

Error:
System.Runtime.InteropServices.COMException was unhandled
ErrorCode=-2147463160
Message="One or more input parameters are invalid
"
Source="Active Directory"
StackTrace:
at Microsoft.VisualBasic.CompilerServices.LateBinding.InternalLateCall(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack, Boolean IgnoreReturn)
at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateCall(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack, Boolean IgnoreReturn)
at STItoSQL.Module1.Main() in C:\Documents and Settings\user9\My Documents\Visual Studio 2005\Projects\STItoSQL\STItoSQL\Module1.vb:line 131
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

----------------------------------------------
Code beginning with loop:

While count < rowcount - 1 'when finished, replace 3 and put in rowcount - 1

userExists = False
myrow = dt.Rows(count)
fname = myrow(0).ToString 'column 0
lname = myrow(1).ToString 'column 1
stnum = myrow(2).ToString 'column 2
schoolNum = myrow(3) 'column 3
schoolNameToBe = schoolIdentify(schoolNum)
schoolLevelToBe = schoolIdentify2(schoolNum)
'MsgBox(fname + " " + " " + lname + " " + stnum + " " + schoolNum + " " + schoolNameToBe + " " + schoolLevelToBe)
'---------------
'In this area, the studentID will be generated
'this takes the first letter of the first name and appends the
'last name then last 4 digits of student number
length = Len(fname)
Try
userID = LCase(fname.Remove(1, length - 1) + lname + stnum.Remove(0, 6))

Catch ff As Exception
MsgBox("argument out of range exception for user " + fname + " " + lname + " " + stnum)
usersMissing = usersMissing + 1
End Try

Try
OU = DE.Children.Find("OU=" + schoolNameToBe + ", OU=" + schoolLevelToBe + ", OU=MCPS_Students")
Catch gg As Exception
Console.WriteLine("error on finding OU " + schoolNameToBe + " " + schoolLevelToBe + " " + fname + " " + lname + " " + schoolNum)
'MsgBox("error on finding OU " + schoolNameToBe + " " + schoolLevelToBe + " " + fname + " " + lname + " " + schoolNum)
End Try
NewUser = OU.Children.Add("CN=" + userID, "User") 'full name
'Dim NewUser2 As DirectoryEntry = OU.Children.Find("CN=userprincipalname", "user") 'full name
NewUser.Properties("sAMAccountName").Value = userID 'user login name pre-windows 2000
NewUser.Properties("userPrincipalName").Add(userID + "@domainName.local") 'user login name
NewUser.Properties("GivenName").Add(fname) 'firstname
NewUser.Properties("sn").Add(lname) 'lastname
NewUser.Properties("displayName").Add(fname + " " + lname) 'display name
NewUser.Properties("description").Add("student") 'description

userACFlags = NewUser.Properties("userAccountControl").Value
NewUser.Properties("userAccountControl").Value = &H220 'userACFlags Or &H200 Xor &H2
NewUser.Properties("PwdLastSet").Value = 0
MsgBox("about to set pw for " + userID)

Try
NewUser.CommitChanges()
Catch ee As Exception
Console.WriteLine("Bombed out on user " + fname + " " + lname + " - " + userID + " " + schoolNameToBe + " " + schoolLevelToBe + ". Program is still running...")
usersMissing = usersMissing + 1
End Try
NewUser.NativeObject.setpassword("testPW")
' NewUser.CommitChanges()
count = count + 1

End While
 
Back
Top