Link to home
Start Free TrialLog in
Avatar of spheretechinc
spheretechinc

asked on

Adding a user in Active Directory

Hello,

I'm having a problem creating a user in active directory when setting the password. Everything else gets created just fine when i take out the code to set the password, but when i put that back in I get an error. I'm not sure what the problem could be. Any help we be appreciated. Here is the code:

<%
UserAccount = request.form("UserAccount")
FirstName = request.Form("FirstName")
LastName = request.Form("LastName")
Password = request.Form("Password")

FullName = FirstName & " " & LastName


set objADsOpenDSObject = GetObject("LDAP:")

strParentDN = "ou=Test OU,dc=<domainname>,dc=local"

strUser = ("LDAP://" & strParentDN)

Set objAds = objADsOpenDSObject.OpenDSObject(strUser , UserName, Password, ADS_USE_ENCRYPTION)



UserAccount = UserAccount + <Domain>

' Create the object in the container using the full user name
Set adsUser = objAds.Create("user", "cn=" + FullName)

' Set the down-level account name for the user (<20 characters)
adsUser.Put "sAMAccountName", FullName

' Set the UPN for the user
adsUser.Put "userPrincipalName", UserAccount


' Update server with required properties
adsUser.SetInfo



' Use IADsUser properties to set other pieces of data
' Refresh the local property cache with new user info
adsUser.GetInfo

' Set the user password.  
adsUser.SetPassword Password----------right here is where I get the problem

' Require the user to change password on login
'adsUser.Put "pwdLastSet", 0

' Enable the account (the default when created is disabled)
adsUser.AccountDisabled = False

' Set the display name of the user
adsUser.FullName = FullName

' Set name information of the user
adsUser.FirstName = FirstName
adsUser.LastName = LastName

adsUser.SetInfo


Set adsUser = Nothing
set objAds = nothing


%>

Avatar of ramesh12
ramesh12

Try changing the variable password to another name and see. I am not sure but take a look at this url the way they are setting

https://www.experts-exchange.com/questions/20633656/Updating-Active-Directory.html?query=active+directory&searchType=topic
Avatar of spheretechinc

ASKER

Thanks for the quick response. The variable is not the problem though because if i try to pass it without using a variable it still doesn't work. I can also get this exact code to work using vbscript it's just doesn't work when i try to do it through asp pages.
I'm not sure why that's not working either, although I would have the same reservations for using 'password' as a variable name.

Anyways, here's the code I use; it's a slightly older technique, but it still might help you:


Function addUser(strUser,strDomain,strFullName,strPassword,strDesc)
   On Error Resume Next
   Dim Domain,User
   Set Domain = Getobject("WinNT://" & strDomain)
   Set User = Domain.Create("User",strUser)
   User.SetInfo
   If Err <> 0 then ' User already exists
      addUser = strUser & " already exists!!<BR>Aborting..."
      Exit Function
   End If
   User.FullName = strFullName
   User.Description = strDesc
   call User.SetPassword(strPassword)
   User.SetInfo
   If Err <> 0 then ' error setting info
      addUser = "Error setting fullname, description, or pw for:<b> " & strDomain & " \" & strUser & "</b>, but user was still created."
      Exit Function
   End If
   Set User = Nothing
   Set Domain = Nothing
End Function
I agree with you about the variable for password and have chagned that. I will give your code a try and see if that works. Another quick question for you, does the asp pages have to be ran on a domain controller or does it matter? Thank you for your help.
Well I answered my own question
ASKER CERTIFIED SOLUTION
Avatar of TheKenman
TheKenman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well, actually for somew reason when i used

objAds.changepassword "", <newpasswprd>

that worked. I have yet to get the set password command to work but it let me set the password using the changepassword command.
Don't know if you were able to solve the problem:
Here is what you can do , works for me,

objAds.Put "userPassword", strPassword

objAds.SetPassword strpassword
objAds.setinfo

-Shibu.
Addendum:

instead of strpassword , you may use password

-Shibu